鉴于这些结构:
type InitRequest struct {
ListenAddr string
ForceNewCluster bool
Spec Spec
}
type Spec struct {
Annotations
AcceptancePolicy AcceptancePolicy `json:",omitempty"`
//...
}
type AcceptancePolicy struct {
Policies []Policy `json:",omitempty"`
}
type Policy struct {
Role NodeRole
Autoaccept bool
Secret *string `json:",omitempty"`
}
此代码无法编译,在复合文字中缺少类型的那一行退出。跟随Go, Golang : array type inside struct, missing type composite literal,但同样的错误:
swarm, err := cli.SwarmInit(context.Background(), swarm.InitRequest{
ListenAddr: "0.0.0.0:2377",
ForceNewCluster: true,
Spec: {
AcceptancePolicy: {
Policies: []Policy{
Policy: {
Role: "manager",
Autoaccept: true,
},
},
}, // here
},
})
任何提示都会非常有用,谢谢!
答案 0 :(得分:6)
我发现您的代码存在一些问题:
AcceptancePolicy
中的“政策”字段是切片而不是地图AcceptancePolicy
或Spec
。Role
是NodeRole
,不是字符串以下是您实施上述修补程序的代码:
mySwarm, err := cli.SwarmInit(context.Background(), swarm.InitRequest{
ListenAddr: "0.0.0.0:2377",
ForceNewCluster: true,
Spec: swarm.Spec{
AcceptancePolicy: swarm.AcceptancePolicy{
Policies: []swarm.Policy{
{
Role: some.conversion.to.NodeRole("manager"),
Autoaccept: true,
},
},
}, // here
},
})