I would like to set a default value and if this is not set in a structure, I would like to set it. It's a bit confusing, but please take a look to the (simplified) code:
package main
import "log"
type Something struct {
A string
B map[string]Type
C Epyt
}
type Type struct {
A Epyt
B string
}
type Epyt struct {
A string
B string
}
func main() {
var a Something
a.A = "Test A (Something)"
// var a.B["one"] Type
a.B["one"].A.A = a.B["one"].A.A
a.B["one"].A.A = "Test A ([one]Type.Epyt)"
a.B["two"].A.A = "Test A ([two]Type.Epyt)"
a.C.A = "Test A (Epyt)"
a.C.B = "Test B (Epyt)"
for i := range a.B {
if a.B[i].A.B == "" {
a.B[i].A.B = a.C.B
}
}
log.Printf("%+v", a)
}
I'm working with viper to unmarshal a config file, therefor I'm not able to use pointers (or am I wrong?).
The error I'm getting, is cannot assign to struct field *** in map.
I found that this is an old bug in go lang still not corrected.
I've read Why do I get a "cannot assign" error when setting value to a struct as a value in a map? and Golang: I have a map of structs. Why can't I directly modify a field in a struct value? but as I said, I can't operate with pointers (or?), therefor please don't mark this as duplicate as it is not!
If someone has an idea on how to do this right, please help!