I'm new working on golang, In this case I have a map [string] which has a struct.
At this point everything is works.
But I would like to have a map[string] in which I can access at the same time to another map[string] which has it self struct.
This is the code in which I'm trying to work.
type myStruct struct{
atrib1 string
atrib2 string
}
var apiRequest map[string] map[string]myStruct
I would like acces to something like this:
func main() {
apiRequest = make(map[string] map[string]myStruct)
apiKeyTypeRequest["Key"]["MyFirstOption"].atrib1 = "first Value first op"
apiKeyTypeRequest["Key"]["MyFirstOption"].atrib2 = "second Value first op"
apiKeyTypeRequest["Key"]["MysecondtOption"].atrib1 = "first Value second op"
}
An alternative to using a map inside a map, is to have a single map[mapKey] where mapKey is a struct:
type mapKey struct {
Key string
Option string
}
The benefits is that you only have to do a single lookup when searching for a myStruct, and you only have to create a single map.
The downside is in case you need to be able to get that options map[string]myStruct map, since it does not exist. Also, you cannot check if a certain key exists or not, because keys and options exists in pairs.
Working example:
package main
import "fmt"
type myStruct struct {
atrib1 string
atrib2 string
}
type mapKey struct {
Key string
Option string
}
func main() {
apiKeyTypeRequest := make(map[mapKey]myStruct)
apiKeyTypeRequest[mapKey{"Key", "MyFirstOption"}] = myStruct{"first Value first op", "second Value first op"}
apiKeyTypeRequest[mapKey{"Key", "MysecondtOption"}] = myStruct{atrib1: "first Value second op"}
fmt.Printf("%+v\n", apiKeyTypeRequest)
}
Playground: http://play.golang.org/p/tGd7ja7QI2
To expand upon previous answers, each map must be declared and instantiated (as well as the struct at the end of the map), that means you'll need to instantiate the "outer" map
mapOfMaps := make(map[string]map[string]myStruct)
as well as the "inner" map(s) for each key you have.
mapOfMaps[key] = make(map[string]myStruct)
The obvious issue you run into here is how do you dynamically check to see if the mapOfMaps[key] has already been instantiated? You do this using the following syntax:
if _, ok := mapOfMaps[key]; !ok {
mapOfMaps[key] = make(map[string]myStruct)
}
This syntax checks to see if mapOfMaps already has an instantiated value for that key, and if not instantiates it. Similarly, you can use the same syntax to instantiate the structs you are using by checking to see of mapOfMaps[key][key2] has been instantiated yet.
if _, ok := mapOfMaps[key][key2]; !ok {
mapOfMaps[key][key2] = new(myStruct)
}
Now you can access the struct through the call to 2 keys and an attribute of the struct
fmt.Println(mapOfMaps[key][key2].atrib1)
As #FUZxxl has said, you need to initialize the sub-map for each outer map, but there is a shorthand syntax for it:
type myStruct struct {
atrib1 string
atrib2 string
}
func main() {
var myMap = map[string]map[string]myStruct{
"foo": {
"bar": {attrib1: "a", attrib2: "b"},
"baz": {"c", "d"}, //or by order...
},
"bar": {
"gaz": {"e", "f"},
"faz": {"g", "h"},
},
}
fmt.Println(myMap["foo"]["bar"].atrib1)
fmt.Println(myMap["bar"]["gaz"].atrib2)
}
Related
I saw several questions asking on how to merge unique structs and how to merge identical structs.
But how would I merge structs that have some overlap? and which fields get taken & when?
e.g.:
type structOne struct {
id string `json:id`
date string `json:date`
desc string `json:desc`
}
and
type structTwo struct {
id string `json:id`
date string `json:date`
name string `json:name`
}
how would I merge it such that I get
{
id string `json:id`
date string `json:date`
desc string `json:desc`
name string `json:name`
}
also, what happens if in this case the two id's are the same (assuming a join over id's) but the names are different?
In javascript, doing something like Object.assign(structOne, structTwo).
Go is a strongly typed language, unlike javascript you can't merge two struct into one combined struct because all type are determined at compile-time. You have two solution here :
Using embedded struct:
One great solution is to use embedded struct because you don't have to merge anything anymore.
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
)
// Shared field
type common struct {
ID string `json:id`
Date string `json:date`
}
type merged struct {
// Common field is embedded
common
Name string `json:name`
Desc string `json:desc`
}
func main() {
buf := bytes.Buffer{}
buf.WriteString("{ \"id\": \"1\", \"date\": \"27/07/2020\", \"desc\": \"the decription...\" }")
merged := &merged{}
err := json.Unmarshal(buf.Bytes(), merged)
if err != nil {
log.Fatal(err)
}
// Look how you can easily access field from
// embedded struct
fmt.Println("ID:", merged.ID)
fmt.Println("Date:", merged.Date)
fmt.Println("Name:", merged.Name)
fmt.Println("Desc:", merged.Desc)
// Output:
// ID: 1
// Date: 27/07/2020
// Name:
// Desc: the decription...
}
If you want to read more about struct embedding:
golangbyexample.com
travix.io
Using Maps
Another solution is to use maps but you will loose the benefits of struct and methods. This example is not the simplest but there is some great example in the other responses.
In this example I'm using Mergo. Mergo is library that can merge structs and map. Here it is used for creating maps object in the Map methods but you can totally write your own methods.
package main
import (
"fmt"
"log"
"github.com/imdario/mergo"
)
type tOne struct {
ID string
Date string
Desc string
}
// Map build a map object from the struct tOne
func (t1 tOne) Map() map[string]interface{} {
m := make(map[string]interface{}, 3)
if err := mergo.Map(&m, t1); err != nil {
log.Fatal(err)
}
return m
}
type tTwo struct {
ID string
Date string
Name string
}
// Map build a map object from the struct tTwo
func (t2 tTwo) Map() map[string]interface{} {
m := make(map[string]interface{}, 3)
if err := mergo.Map(&m, t2); err != nil {
log.Fatal(err)
}
return m
}
func main() {
dst := tOne{
ID: "destination",
Date: "26/07/2020",
Desc: "destination object",
}.Map()
src := tTwo{
ID: "src",
Date: "26/07/1010",
Name: "source name",
}.Map()
if err := mergo.Merge(&dst, src); err != nil {
log.Fatal(err)
}
fmt.Printf("Destination:\n%+v", dst)
// Output:
// Destination:
// map[date:26/07/2020 desc:destination object iD:destination name:object name
}
Go structs and JavaScript objects are very different. Go structs do not have dynamic fields.
If you want dynamic key/value sets that you can easily iterate over and merge, and are very JSON friendly, why not a map[string]interface{}?
$ go run t.go
map[a:1 b:4]
map[a:1 b:4 c:3]
$ cat t.go
package main
import(
"fmt"
)
type MyObj map[string]interface{}
func (mo MyObj)Merge(omo MyObj){
for k, v := range omo {
mo[k] = v
}
}
func main() {
a := MyObj{"a": 1, "b": 4}
b := MyObj{"b": 2, "c": 3}
b.Merge(a)
fmt.Printf("%+v\n%+v\n", a, b)
}
you can use github.com/fatih/structs to convert your struct to map. Then iterate over that map and choose which fields need to copy over. I have a snippet of code which illustrates this solution.
func MergeStruct (a structOne,b structTwo) map[string]interface{}{
a1:=structs.Map(a)
b1:=structs.Map(b)
/* values of structTwo over writes values of structOne */
var myMap=make(map[string]interface{})
for val,key:=range(a1){
myMap[key]=val
}
for val,key:=range(b1){
myMap[key]=val
}
return myMap
}
You can use the reflect package to do this. Try iterating through the two structs and then you can either use another struct type to store the values or maybe use a map.
Check out this question to find out how you can iterate over a struct.
Check out this question to find out how to get the name of the fields.
Remember to use exported field names for the reflect package to work.
Here is an example which works.
I am trying to modify some stuff in Go. I have the following struct I created
I have an API call returning something like this
MyStruct struct {
DID string `bson:"d_id" json:"d_id"`
PID string `bson:"p_id" json:"p_id"`
...
}
in one call in the code, I want to append a new key to that struct
like
myNewStruct {
DID string `bson:"d_id" json:"d_id"`
PID string `bson:"p_id" json:"p_id"`
...
MyNewKey string `bson:"new_key" json:"new_key"`
}
The thing is, I want to add a new key, and keep the rest at the root of the object, like the object above. without having to rewrite the full object structure, or doing a for loop of each key.
type MyNewStruct struct {
*MyStruct
MyNewKey MyValueType
}
I have, two variable with the data,
MyStructData and MyNewKeyData
I want to, but don t know how to merge those two data inside MyNewStruct so that everything in MyStructData will be at the root of the key, and everything in MyNewKeyData will be indise the key MyNewKey
I am trying stuff like
theObjectIWant := MyNewStruct {
MyStructData,
"MyNewKey" : MyNewKeyData
}
but doesn't work
When you create an anonymous member in a struct, the compiler generates a name for the member that is named the same as the type. You can use this name when initializing the containing struct.
Here is a simplified example
type MyStruct struct {
DID string
PID string
}
type MyNewStruct struct {
MyStruct
MyNewKey string
}
ms := MyStruct{
DID: "did",
PID: "pid",
}
m := MyNewStruct{
MyStruct: ms,
MyNewKey: "test",
}
I'm not sure I fully understand what your looking for, but this may help.
type MyStruct struct {
DID string
PID string
}
type myNewStruct struct {
MyStruct
newKey string
}
func main() {
s1 := MyStruct{DID: `bson:"d_id" json:"d_id"`}
s2 := myNewStruct{MyStruct: s1, newKey: `bson:"new_key" json:"new_key"`}
fmt.Println(s2.DID)
fmt.Println(s2.newKey)
}
I'm trying to convert an interface dynamically back to it's original struct but I am having issues accessing attributes of the struct after the conversion.
Take this code for example.
package main
import (
"fmt"
"log"
)
type struct1 struct {
A string
B string
}
type struct2 struct {
A string
C string
}
type struct3 struct {
A string
D string
}
func main() {
s1 := struct1{}
s1.A = "A"
structTest(s1)
s2 := struct2{}
s2.A = "A"
structTest(s2)
s3 := struct3{}
s3.A = "A"
structTest(s3)
}
func structTest(val interface{}) {
var typedVal interface{}
switch v := val.(type) {
case struct1:
fmt.Println("val is struct1")
case struct2:
fmt.Println("val is struct2")
case struct3:
fmt.Println("val is struct3")
default:
log.Panic("not sure what val is.")
}
fmt.Println(typedVal.A)
}
I would like to be able to pass in one of 3 known struct types into my function. Then figure out which struct type was passed in to type assert it. Finally I want to be able to access like attributes.
Basically I want to have some basic inheritance in my structs, but so far it seems that it is not possible to do this in go. I saw some posts mentioning inheritance using an interface, but my structs have no methods so I'm not sure how I would use an interface.
Is something like this possible in go?
I would like to be able to pass in one of 3 known struct types into my function. Then figure out which struct type was passed in to type assert it. Finally I want to be able to access like attributes.
You can use type assertions to do exactly that. Basic idea is, in any case of the type switch just use type assertion to get a concrete instance of the corresponding type and then you can call whatever properties that you wish.
Take a look at the following example
package main
import (
"fmt"
)
type test1 struct {
A, B string
}
type test2 struct {
A, C string
}
func testType(val interface{}) {
switch val.(type) {
case test1:
t := val.(test1)
fmt.Println(t.B)
break
case test2:
t := val.(test2)
fmt.Println(t.C)
break
}
}
func main() {
t1, t2 := test1{B: "hello"}, test2{C: "world"}
testType(t1)
testType(t2)
}
Playground
Function structTest(val interface{}) in your code seems to be loosely typed. You pass it an untyped argument and expect it will satisfy some condition (will have field A), it looks strange in any typed language.
Using an interface this kind of polymorphism, in Go, to my mind, can be expressed something like
package main
import (
"fmt"
"log"
)
type A string
type HasA interface {
PrintA()
}
func (a A) PrintA() { fmt.Println(a) }
type struct1 struct {
A
B string
}
type struct2 struct {
A
C string
}
type struct3 struct {
A
D string
}
func main() {
s1 := struct1{}
s1.A = "A"
structTest(s1)
s2 := struct2{}
s2.A = "A"
structTest(s2)
s3 := struct3{}
s3.A = "A"
structTest(s3)
}
func structTest(val HasA) {
switch val.(type) {
case struct1:
fmt.Println("val is struct1")
case struct2:
fmt.Println("val is struct2")
case struct3:
fmt.Println("val is struct3")
default:
log.Panic("not sure what val is.")
}
val.PrintA()
}
Playground
Apologies for the title, but this is a weird one and out of my ability to understand.
I'm working with a go library that's sort of finished, but sort of not:
https://github.com/yfronto/go-statuspage-api
The statuspage.io API supports the following params when posting an incident:
incident[components][component_id] - Map of status changes to apply to affected components.
An example would be:
"incident[components][ftgks51sfs2d]=degraded_performance"
Unfortunately, the struct defined in the library doesn't support that particular field:
type NewIncidentUpdate struct {
Name string
Status string
Message string
WantsTwitterUpdate bool
ImpactOverride string
ComponentIDs []string
}
func (i *NewIncidentUpdate) String() string {
return encodeParams(map[string]interface{}{
"incident[name]": i.Name,
"incident[status]": i.Status,
"incident[message]": i.Message,
"incident[wants_twitter_update]": i.WantsTwitterUpdate,
"incident[impact_override]": i.ImpactOverride,
"incident[component_ids]": i.ComponentIDs,
})
}
How can I update this struct (and the associated encodeParams function) to support passing an arbitrary map of components and associated statuses?
You could embed a NewIncidentUpdate inside your own struct that defines the component status changes.
type MyIncidentUpdate struct {
NewIncidentUpdate
ComponentStatusChanges map[string]string
}
then define String the same way, while accomodating for your ComponentStatusChanges map.
func (i *MyIncidentUpdate) String() string {
params := map[string]interface{}{
"incident[name]": i.Name,
"incident[status]": i.Status,
"incident[message]": i.Message,
"incident[wants_twitter_update]": i.WantsTwitterUpdate,
"incident[impact_override]": i.ImpactOverride,
"incident[component_ids]": i.ComponentIDs,
}
for k, val := range i.ComponentStatusChanges {
key := fmt.Sprintf("incident[components][%s]", k)
params[key] = val
}
return encodeParams(params)
}
I would have expected this code to work:
package main
type Item struct {
Key string
Value string
}
type Blah struct {
Values []Item
}
func main() {
var list = [...]Item {
Item {
Key : "Hello1",
Value : "World1",
},
Item {
Key : "Hello1",
Value : "World1",
},
}
_ = Blah {
Values : &list,
}
}
I thought this would be the correct way of doing this; Values is a slice, list is an array. &list should be a slice, which is assignable to Item[], right?
...but instead, it errors with the message:
cannot use &list (type *[2]Item) as type []Item in assignment
In C, you'd write:
struct Item {
char *key;
char *value;
};
struct Blah {
struct Item *values;
};
How do you do that in Go?
I saw this question:
Using a pointer to array
...but either the answers are for a previous version of Go, or they're just plain wrong. :/
A slice is not simply a pointer to an array, it has an internal representation which contains its length and capacity.
If you want to get a slice from list you can do:
_ = Blah {
Values : list[:],
}
Go is, fortunately, not so verbose as it might seem from the OP. This works:
package main
type Item struct {
Key, Value string
}
type Blah struct {
Values []Item
}
func main() {
list := []Item{
{"Hello1", "World1"},
{"Hello2", "World2"},
}
_ = Blah{list[:]}
}
(Also here)
PS: Let me suggest to not write C in Go.
When you are starting out with Go ignore arrays completely and just use slices is my advice. Arrays are rarely used and cause Go beginners a lot of trouble. If you have a slice then you don't need a pointer to it since it is a reference type.
Here is your example with a slice and no pointers which is much more idiomatic.
package main
type Item struct {
Key string
Value string
}
type Blah struct {
Values []Item
}
func main() {
var list = []Item{
Item{
Key: "Hello1",
Value: "World1",
},
Item{
Key: "Hello1",
Value: "World1",
},
}
_ = Blah{
Values: list,
}
}