Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

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?

like image 213
jonadev95 Avatar asked Aug 01 '15 11:08

jonadev95


People also ask

Is a struct a map?

Structs are wrappers around maps that provide additional functionality to the maps. Struct is a map under the hood with an additional field called __struct__ . Unlike map, when we create a struct, we can include specific fields or provide a default value to some fields.

How do you create a map in go?

Go by Example: Maps Maps are Go's built-in associative data type (sometimes called hashes or dicts in other languages). To create an empty map, use the builtin make : make(map[key-type]val-type) . Set key/value pairs using typical name[key] = val syntax. Printing a map with e.g. fmt.


2 Answers

cc := &dockerclient.ContainerConfig{
    // ...
    Volumes: map[string]struct{}{
        "foo": struct{}{},
        "bar": struct{}{},
        // ...
    },
}
like image 139
Ainar-G Avatar answered Sep 20 '22 14:09

Ainar-G


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

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

like image 39
Tamil Avatar answered Sep 21 '22 14:09

Tamil