Golang : automatically figure out array length(size) with three dots
In Go, you can specify a length(size) of an array either explicitly or implicitly(i.e let the compiler figure out) with the three dots [...]
.
However, before we proceed further, it is good to know the tiny difference between array and slice.
Arrays length are fixed during run time. Slices are not bounded by their length and values from one slice can be passed to another even when their lengths are different.
These are ARRAYS
// Explicit length. You specified it
var days := [7]string { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }
// Implicit length with the three dots. (Compiler will determine the length)
var days := [...]string { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }
This is a SLICE
var days := []string { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" }
The three dots [...] can be useful in situation when you wanted to create an array based on content from files or streams, but do not know the length until run time.
Hope this simple tutorial can be useful to you.
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+27.9k PHP : Count number of JSON items/objects
+6.3k Golang : Get missing location after unmarshal binary and gob decode time.
+4.4k Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
+6.6k Golang : Spell checking with ispell example
+9.8k Golang : Eroding and dilating image with OpenCV example
+9k Golang : Find network service name from given port and protocol
+22.2k Golang : Use TLS version 1.2 and enforce server security configuration over client
+8.1k Swift : Convert (cast) String to Float
+15.4k JavaScript/JQuery : Detect or intercept enter key pressed example
+5.6k Golang : Display advertisement images or strings on random order
+41.5k Golang : How to count duplicate items in slice/array?
+17.6k Golang : Linked list example