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
+11.3k Swift : Convert (cast) Float to String
+35.5k Golang : Get file last modified date and time
+6k Javascript : Generate random key with specific length
+38.8k Golang : How to iterate over a []string(array)
+11.1k Golang : Byte format example
+4.9k Golang : Issue HTTP commands to server and port example
+6.3k Golang : How to determine if request or crawl is from Google robots
+11.6k Golang : Find and draw contours with OpenCV example
+4.7k Unix/Linux : secure copying between servers with SCP command examples
+7.5k Golang : Reverse a string with unicode
+7.6k Swift : Convert (cast) String to Double
+15.6k Golang : Read a file line by line