In a pub quiz by Dave Cheney I came across the following construct:
a := [...]int{5, 4: 1, 0, 2: 3, 2, 1: 4}
fmt.Println(a)
>> [5 4 3 2 1 0]
(Playground Link)
It seems you can use keys in the initialization fields of an array (4: 1, 0
means set element at index 4 to 1, element at index 5 to 0). I have never seen something like this before. What is its use case? Why not set the particular index directly?
You can initialize an array with pre-defined values using an array literal. An array literal have the number of elements it will hold in square brackets, followed by the type of its elements. This is followed by a list of initial values separated by commas of each element inside the curly braces.
To create String Array, which is to declare and initialize in a single line, we can use the syntax of array to declare and initialize in one line as shown in the following. arrayName is the variable name for this string array. arraySize is the number of elements we would like to store in this string array.
To add an element to a slice in go, we can use the append() method. The append() method takes the name of the slice and the new element to add. In the example above, we add the element “David” to the slice “my_slice”.
In the code above: Line 3: We import the fmt package. Line 7: Inside the main() function, we use := to declare an array marks of type int and length 5 . Line 10: We output the marks array on the console.
In composite literals the key (index in case of array and slice literals) can be optionally provided.
For array and slice literals the following rules apply:
- Each element has an associated integer index marking its position in the array.
- An element with a key uses the key as its index; the key must be a constant integer expression.
- An element without a key uses the previous element's index plus one. If the first element has no key, its index is zero.
Elements get the zero value of the element type whose value is not specified.
You can use this to:
more compactly initialize arrays and slices if the array/slice has many zero values and just a few non-zero values
skip ("jump over") contiguous parts when enumerating elements, and the skipped elements will be initialized with the zero values
specify the first couple of elements, and still specify the length (max index + 1) you want the array/slice to have:
a := []int{10, 20, 30, 99:0} // Specify first 3 elements and set length to 100
The spec also contains an example: create an array which tells if a character is a vowel. This is a very compact and talkative way to initialize the array:
// vowels[ch] is true if ch is a vowel
vowels := [128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true}
Another example: let's create a slice which tells if a day is weekend; Monday being 0, Tuesday being 1, ... and Sunday being 6:
weekend := []bool{5: true, 6: true} // The rest will be false
Or even better, you can even omit the 2nd index (6
) as it will be implicitly 6
(previous +1):
weekend := []bool{5: true, true} // The rest will be false
If your array indices are sparse, it's shorter than doing {1,0,0,0,0,2,0,0,0,0,3}
etc, and it's shorter than multiple assignment lines, so I'm guessing that's the use case.
I've never seen this syntax used before anywhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With