无法构建简单的 Golang 代码 – 切片字面量语法

huangapple go评论94阅读模式
英文:

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

huangapple
  • 本文由 发表于 2015年10月2日 13:20:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/32900985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定