Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is the content of slice not changed in GO?

Tags:

slice

go

I thought that in GO language, slices are passed by reference. But why the following code doesn't change the content of slice c? Am I missing something? Thank you.

package main

import (
    "fmt"
)


func call(c []int) {
    c = append(c, 1)
    fmt.Println(c)
}

func main() {
    c := make([]int, 1, 5)
    fmt.Println(c)
    call(c)
    fmt.Println(c)
}

The result printed is:

[0] [0 1] [0]

while I was expecting

[0] [0 1] [0 1]

like image 371
Kid Avatar asked Dec 11 '22 22:12

Kid


1 Answers

The length of the slice is kept in the slice header which is not passed by reference. You can think of a slice as a struct containing a pointer to the array, a length, and a capacity.

When you appended to the slice, you modified index 1 in the data array and then incremented the length in the slice header. When you returned, c in the main function had a length of 1 and so printed the same data.

The reason slices work this way is so you can have multiple slices pointing to the same data. For example:

x := []int{1,2,3}
y := x[:2] // [1 2]
z := x[1:] // [2 3]

All three of those slices point to overlapping data in the same underlying array.

like image 172
Stephen Weinberg Avatar answered Jan 03 '23 03:01

Stephen Weinberg