Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

x := [...]string{"Sat", "Sun"} vs x:= []string{"Sat", "Sun"}

In the go lang spec they used three dots in one of the examples:

days := [...]string{"Sat", "Sun"}  // len(days) == 2

Does it make any difference if the three dots are left out?

like image 628
robert king Avatar asked Nov 27 '13 23:11

robert king


People also ask

What is [] string Golang?

string in Golang is a set of all strings that contain 8-bit bytes. By default, strings in Golang are UTF-8 encoded. Variable of type string is enclosed between double-quotes. The type string variable's value is immutable. The value assigned to the type string variable can be empty but cannot be nil.

What is difference between array and slice in Golang?

Slices in Go and Golang The basic difference between a slice and an array is that a slice is a reference to a contiguous segment of an array. Unlike an array, which is a value-type, slice is a reference type. A slice can be a complete array or a part of an array, indicated by the start and end index.

How do you declare an array of integers in Golang?

Declaring an Array in Golang By default, all the array elements are initialized with the zero value of the corresponding array type. For example, if we declare an integer array, all the elements will be initialized with 0 .


1 Answers

It makes a pretty big difference: The difference is between an array and a slice.

[]string creates a slice that points to an array of strings. On the other hand, [...] creates an actual array of strings.

There is a great blog post about the difference between the two on the golang blog. I'll try to summarize here as best I can.

Arrays in golang are like value types, they are references to a specific type and are always of a specific length. There are two ways to create an array: 1) with explicit length and 2) implicit length:

// Explicit length. 
var days := [7]string { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }

// Implicit length. (Let the compiler figure it out how long it is)
var days := [...]string { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" } 

These are both equivalent array definitions. Note that the length of an array is part of it's type definition. So, you can't interchange arrays of similar type with different lengths:

// These two are not interchangeable!
var someArray [5]string;
var otherArray [10]string;

func work(data [5]string) { /* ... */ }

work(someArray)  // good
work(otherArray) // not so good

Also note that arrays, like structs, are passed as value – a copy of the array will be given to a function, not a reference to it..

Slices, on the other hand, are like reference types. They are backed by an array, but they are more malleable. They include a pointer to a position within array, a length, and a capacity.

// Create a slice
var days := []string { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }

Slices, unlike arrays, are not explicitly bound to their length and slices of different lengths can be passed for one another. They also act more like a pointer, which means they get passed by reference, instead of value.

There is also a great post about Go Data Structures, and how they are they are represented in memory. I highly recommend checking it out.

like image 132
J. Holmes Avatar answered Oct 17 '22 15:10

J. Holmes