英文:
Cannot build simple golang code - Slice literal syntax
问题
我正在尝试使用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: []string{"FOO=foo"},
Image: "redis",
}
opts := docker.CreateContainerOptions{
Config: config,
HostConfig: h,
}
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 }
我似乎没有找到语法上的错误(尽管我是个新手)。问题是什么?
英文:
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
得分: 2
切片字面值应该像这样:
[]string{"a", "b", "c"}
而不是像这样:
["a", "b", "c"]
所以将这个代码片段:
Env: ["FOO=foo"],
改为:
Env: []string{"FOO=foo"},
英文:
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"},
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论