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

时间:2015-10-02 05:20:57

标签: go

我正在尝试使用go-dockerclient

构建一个简单的golang程序
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)
}

这给了我:

:~/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 }

我似乎没有发现语法有任何问题(不过确实是一个菜鸟)。有什么问题?

1 个答案:

答案 0 :(得分:2)

切片文字应如下所示:

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

不喜欢这样:

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

所以改变这个:

Env: ["FOO=foo"],

对此:

Env: []string{"FOO=foo"},