doz97171 2016-07-14 19:19
浏览 338
已采纳

golang中的“复合文字中的缺少类型”

Given these structs:

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"`
}

This code doesn't compile, exiting on that line with missing type in composite literal. Followed Go, Golang : array type inside struct, missing type composite literal , but same error with:

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
    },
})

Any hint will be very helpful, thanks!

  • 写回答

1条回答 默认 最新

  • doraemon0769 2016-07-14 19:23
    关注

    I identified a few issues with your code:

    1. Your Policies field in AcceptancePolicy is a slice not a map
    2. You didn't specify the types of the AcceptancePolicy or the Spec.
    3. You're naming a variable the same as the import package.
    4. Role is a NodeRole, not a string

    Here's your code with the above fixes implemented:

    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
        },
    })
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私