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
// vowels[ch] is true if ch is a vowel
vowels := [128]bool{'a': true, 'e': true, 'i': true, 'o': true, 'u': true, 'y': true}
weekend := []bool{5: true, 6: true} // The rest will be false
甚至更好的是,您可以省略第二个索引(6
),因为它会隐式地变成6
(前一个+1):
weekend := []bool{5: true, true} // The rest will be false
{1,0,0,0,0,2,0,0,0,0,3}
等方式会更长,同时也比多个赋值语句更长,因此我猜测这就是使用情况。我之前从未在任何地方见过这种语法。var businessDays = [7]bool{
time.Monday: true,
time.Tuesday: true,
time.Wednesday: true,
time.Thursday: true,
time.Friday: true,
}