duanmanmian7589 2015-10-02 05:20
浏览 34
已采纳

无法构建简单的golang代码-切片文字语法

I am trying to build a simple golang program using go-dockerclient

package main

import (
        docker "github.com/fsouza/go-dockerclient"
)

func main () {
        h := &docker.HostConfig {
                Memory: 4194304,
                MemorySwap: -1,
                CPUShares: 5,
                NetworkMode: "host",
        }
        client, err := docker.NewClient("unix:///var/run/docker.sock")
        config := &docker.Config {
                Env: ["FOO=foo"],
                Image: "redis",
        }
        opts := docker.CreateContainerOptions {
                Config: config,
                HostConfig: hostConfig,
        }
        container, _ := client.CreateContainer(opts)
        err = client.StartContainer(container.ID)
}

This gives me:

:~/gosrc/src/github.com/achanda$ go build
# github.com/achanda
./main.go:16: syntax error: unexpected comma
./main.go:22: non-declaration statement outside function body
./main.go:23: non-declaration statement outside function body
./main.go:24: non-declaration statement outside function body
./main.go:25: syntax error: unexpected }

I don't seem to find anything wrong with the syntax (admittedly a noob though). What is the problem?

  • 写回答

1条回答 默认 最新

  • douzhizao0270 2015-10-02 05:29
    关注

    Slice literals should look like this:

    []string{"a", "b", "c"}
    

    Not like this:

    ["a", "b", "c"]
    

    So change this:

    Env: ["FOO=foo"],
    

    To this:

    Env: []string{"FOO=foo"},
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度