I cannot figure out how to initialize a nested struct. Find an example here: http://play.golang.org/p/NL6VXdHrjh
package main
type Configuration struct {
Val string
Proxy struct {
Address string
Port string
}
}
func main() {
c := &Configuration{
Val: "test",
Proxy: {
Address: "addr",
Port: "80",
},
}
}
If you don't want to go with separate struct definition for nested struct and you don't like second method suggested by @OneOfOne you can use this third method:
You can check it here: https://play.golang.org/p/WoSYCxzCF2
Define your
Proxy
struct separately, outside ofConfiguration
, like this:See http://play.golang.org/p/7PELCVsQIc
You need to redefine the unnamed struct during
&Configuration{}
https://play.golang.org/p/Fv5QYylFGAY
You can define a struct and create its object in another struct like i have done below:
Hope it helped you :)
You have this option also:
Well, any specific reason to not make Proxy its own struct?
Anyway you have 2 options:
The proper way, simply move proxy to its own struct, for example:
The less proper and ugly way but still works: