The correct way to initialize an empty slice

To declare an empty fragment with an unfixed size, it is better to do:

mySlice1 := make([]int, 0) 

or:

 mySlice2 := []int{} 

Just wondering which one is right.

+167
arrays slice go
Mar 20 '15 at 10:27
source share
5 answers

The two options you gave are semantically identical, and I would suggest that they produce the same assembly instructions.

To avoid unnecessary highlighting, in case you end up not using a slice, you can leave it with a nil value:

 var myslice []int 

As written on the Golang.org blog :

a zero slice is functionally equivalent to a slice of zero length, even if it does not indicate anything. It has zero length and can be added with selection.

+211
Mar 20 '15 at 10:38
source share

They are equivalent. See this code:

 mySlice1 := make([]int, 0) mySlice2 := []int{} fmt.Println("mySlice1", cap(mySlice1)) fmt.Println("mySlice2", cap(mySlice2)) 

Exit:

 mySlice1 0 mySlice2 0 

Both slices have a capacity of 0 which means that both slices have a length of 0 (cannot be larger than the capacity), which means that both slices have no elements. This means that 2 slices are identical in every aspect.

See related questions:

What is the point of having a zero slice and an empty slice in the Golang?

zero slices versus non zero slices versus empty slices in Go language

+51
Mar 20 '15 at 10:37
source share

As a complement to @ANisus' answer ...

The following is some information from the book, "Go in Action," which I think is worth mentioning:

The difference between nil and empty slices

If we think about such a fragment:

 [pointer] [length] [capacity] 

then

 nil slice: [nil][0][0] empty slice: [addr][0][0] // points to an address 

nil slice

Theyre useful when you want to represent a fragment that does not exist, for example, when an exception occurs in a function that returns a slice.

 // Create a nil slice of integers. var slice []int 

empty fragment

Empty fragments are useful when you want to represent an empty collection, for example, when a database query returns null results.

 // Use make to create an empty slice of integers. slice := make([]int, 0) // Use a slice literal to create an empty slice of integers. slice := []int{} 

Regardless of whether you use a nil fragment or an empty fragment, the built-in functions append , len and cap work the same.




Go to the playground :

 package main import ( "fmt" ) func main() { var nil_slice []int var empty_slice = []int{} fmt.Println(nil_slice == nil, len(nil_slice), cap(nil_slice)) fmt.Println(empty_slice == nil, len(empty_slice), cap(empty_slice)) } 

prints:

 true 0 0 false 0 0 
+31
Sep 01 '17 at 9:50
source share

An empty slice and a zero slice are initialized differently in Go:

 var nilSlice []int emptySlice1 := make([]int, 0) emptySlice2 := []int{} fmt.Println(nilSlice == nil) // true fmt.Println(emptySlice1 == nil) // false fmt.Println(emptySlice2 == nil) // false 

As with all three slices, len and cap are set to 0.

+11
Mar 21 '17 at 2:04 on
source share

I was confused about the words:

To avoid unnecessary selection, in the event that you do not end up using a slice, you can leave it with a zero value.

Since the outputs below are the same, so I don't have a guest with an empty slice.

  var a = []int{} var b = []string{} var c = []interface{}{} fmt.Printf("%p\n", a) fmt.Printf("%p\n", b) fmt.Printf("%p\n", c) 

exit:

0xd301f0 0xd301f0 0xd301f0

0
Jul 12 '19 at 12:45
source share



All Articles