Why is the content of slice not changed in GO?
Posted
by
Kid
on Stack Overflow
See other posts from Stack Overflow
or by Kid
Published on 2012-09-05T03:37:04Z
Indexed on
2012/09/05
3:37 UTC
Read the original article
Hit count: 190
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]
© Stack Overflow or respective owner