I'm trying to get array
changed in other func
, and then return the changed. So I wrote a sample code below, my question is why changes in changeArr02
function can affect the origin arr
while changeArr01
not.
package main
import "fmt"
func changeArr01(arr [1]int) {
arr[0] = -100
}
func changeArr02(arr []int) {
arr[0] = -200
}
func main() {
arr01 := [1]int{}
changeArr01(arr01)
fmt.Println(arr01) // 0
arr02 := []int{0}
changeArr02(arr02)
fmt.Println(arr02) // -200
}
arr01 := [1]int{}
is an array of integer with the size of 1
arr02 := []int{0}
it's a slice of ints with one integer inside (0)
more explained in here: https://tour.golang.org/moretypes/7