Spec: Composite literals:
The key is interpreted as a field name for struct literals, an index for array and slice literals, and a key for map literals. For map literals, all elements must have a key. It is an error to specify multiple elements with the same field name or constant key value.
So no, using a composite literal to initialize a map
, we can't omit the keys.
We may (and usually we do) omit indices when using a slice literal. So we may list key-value pairs in one map literal (where we know or want to explicitly state the key), and list elements with subsequent indices in another slice literal to initialize a slice. Then we can iterate over the slice and add key-value pairs based on index-value pairs.
Your first example:
m := map[interface{}]interface{}{
"name": "attn",
}
s := []int{1, 5, 6, 7, 8}
for i, v := range s {
m[i] = v
}
fmt.Println(m)
Output:
map[0:1 1:5 2:6 3:7 4:8 name:attn]
Your other example:
m = map[interface{}]interface{}{
0: "start", "name": "mattn", "age": 39,
"child": []int{1: 1, 2, 3, 4, 5, 9: 1},
}
fmt.Println(m)
Output:
map[0:start name:mattn age:39 child:[0 1 2 3 4 5 0 0 0 1]]
Try these on the Go Playground.
Note that in the 2nd example the value for "child"
is a slice which contains 0
elements. The reason for this is because a slice or array is contiguous, even if we don't provide elements for all indices, there will still be elements for the missing indices. If we don't want this, that is not a slice (or array), but that is a map
, and we can initialize it as seen in the first example.
We can't distinguish between an implicit 0
and an explicit 0
(which we may list in the composite literal). If 0
is a value we may use, another alternative would be to define type of child
to be []interface{}
, and if we don't explicitly specify a value in the composite literal, it will be nil
.
See this question for more examples on initializing arrays and slices: Keyed items in golang array initialization