我无法弄清楚如何初始化嵌套结构.在这里找一个例子: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",
},
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试从字面上初始化GO中的以下结构:
这是结构:
type tokenRequest struct {
auth struct {
identity struct {
methods []string
password struct {
user struct {
name string
domain struct {
id string
}
password string
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
req := &tokenRequest{
auth: struct {
identity: struct {
methods: []string{"password"},
password: {
user: {
name: os.Username,
domain: {
id: "default",
},
password: os.Password,
},
},
},
},
}
Run Code Online (Sandbox Code Playgroud)
https://play.golang.org/p/e8Yuk-37_nN
我可以初始化,而无需单独定义所有嵌套的结构(即auth,identity,password,user)
谢谢。