What's the difference between func foo(arr []int) int
and func foo(arr [*num*]int) int
?
Here are two examples:
func foo1(arr [2]int) int {
arr[0] = 1
return 0
}
func foo2(arr []int) int {
arr[0] = 1
return 0
}
func main() {
var arr1 = [2]int{3, 4}
var arr2 = []int{3, 4}
foo1(arr1)
println(arr1[0]) // result is 3, so arr in foo1(arr) is a copy
foo2(arr2)
println(arr2[0]) // result is 1, so arr in foo2(arr) is not a copy, it is a reference
}
I also found if I use foo1(arr2)
or foo2(arr1)
, the compiler will report an error like "cannot use arr2 (type []int) as type [2]int in argument to foo1" and "cannot use arr1 (type [2]int) as type []int in argument to foo2".
So who can help to explain what's the difference between them or give me some link to study? Thank you in advance.