创建一个 map[string]struct{} 并赋值。

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

create map[string]struct{} and assign a value

问题

你可以使用以下方法为map赋值:

containerConfig := &dockerclient.ContainerConfig{
    Volumes: make(map[string]struct{}),
}

containerConfig.Volumes["volume1"] = struct{}{}
containerConfig.Volumes["volume2"] = struct{}{}

在上面的示例中,我们首先创建了一个空的Volumes map,然后使用键值对的方式为map赋值。每个键都是字符串类型,值是一个空的struct{}。你可以根据需要添加任意数量的键值对。

英文:

I'm using the github.com/samalba/dockerclient and want to create a Container.
So, the method is CreateContainer, which needs a ContainerConfig.

The ContainerConfig is a struct. And there's a field Volumes, the type of which is type map[string] struct{}.

I know that I could create such a map with make(map[string]struct{})

But how do I assign values to the map?

答案1

得分: 17

cc := &dockerclient.ContainerConfig{
// ...
Volumes: map[string]struct{}{
"foo": struct{}{},
"bar": struct{}{},
// ...
},
}

英文:
cc := &dockerclient.ContainerConfig{
    // ...
    Volumes: map[string]struct{}{
        "foo": struct{}{},
        "bar": struct{}{},
        // ...
    },
}

答案2

得分: 3

卷:map[string]struct{}{
"dir1": struct{}{},
"dir2": struct{}{},
}

仅将本地主机上的文件夹映射到Docker容器中。不会映射任何内容。

英文:

Volumes: map[string]struct{}{
"dir1": struct{}{},
"dir2": struct{}{},
},

Maps only the folder from localhost to docker container. No contents will be mapped.

答案3

得分: 0

在Go 1.19中,可以通过删除冗余的struct{}来简化语法:

cc := &dockerclient.ContainerConfig{
    // ...
    Volumes: map[string]struct{}{
        "foo": {},
        "bar": {},
        // ...
    },
}
英文:

In Go 1.19, syntax can be simplified by removing redundant struct{}:

cc := &dockerclient.ContainerConfig{
    // ...
    Volumes: map[string]struct{}{
        "foo": {},
        "bar": {},
        // ...
    },
}

huangapple
  • 本文由 发表于 2015年8月1日 19:38:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/31761391.html
匿名

发表评论

匿名网友

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

确定