itemprop="text">
I cannot figure out how to initialize
a nested struct. Find an example here:
href="http://play.golang.org/p/NL6VXdHrjh">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",
},
}
}
Answer
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:
type
Configuration struct {
Val string
Proxy
Proxy
}
type Proxy struct {
Address
string
Port string
}
func main()
{
c := &Configuration{
Val:
"test",
Proxy: Proxy{
Address: "addr",
Port:
"port",
},
}
fmt.Println(c)
fmt.Println(c.Proxy.Address)
}
The
less proper and ugly way but still
works:
c :=
&Configuration{
Val: "test",
Proxy: struct {
Address string
Port string
}{
Address:
"addr",
Port: "80",
},
}
No comments:
Post a Comment