jonadev95
jonadev95

Reputation: 367

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

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?

Upvotes: 13

Views: 29821

Answers (3)

Fedorov7890
Fedorov7890

Reputation: 1255

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

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

Upvotes: 0

Tamil
Tamil

Reputation: 201

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

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

Upvotes: 3

Ainar-G
Ainar-G

Reputation: 36259

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

Upvotes: 17

Related Questions