Given this anonymous struct:
var myMap struct {
list map[string]string
someStuff *some.Object
}
Can I initialize the list
and someStuff
in one declaration?
This doesn't work:
var myMap struct {
list map[string]string = make(map[string]string)
someStuff *some.Object = &some.Object{}
}
Here you go:
var myMap = struct {
list map[string]string
str string
}{
list: map[string]string{"hello":"string"},
str: "myString",
}
You can also do it this way:
var myMap = struct {
list map[string]string
str string
}{map[string]string{"hello":"string"}, "myString"}
And a working example: Go PlayGround.
So you declare your structure and then in curly braces you initiate it. (I learned it from this old go talk.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With