In most languages (like c++) passing arrays result in implicitly passing it by a reference, so any changes to the passed array in the function will result in changing the original one. I am learning Golang, and In the book "The Go Programming Language" by Alan A.A. Donovan and Brian W. Kernighan It is said, that its behaviour is different from other languages - It does not implicitly pass array by reference.
It is confusing me a bit - doesn't that mean that passing an array without the reference should not modify the array itself? Let me illustrate that:
func main() {
tab := []int{1, 2, 3}
fmt.Println(tab)
// Results in [1 2 3]
reverse(tab)
fmt.Println(tab)
// Results in [3 2 1]
}
func reverse(tab []int) {
for i, j := 0, len(tab)-1; i < j; i, j = i+1, j-1 {
tab[i], tab[j] = tab[j], tab[i]
}
}
In code above array is not passed by the reference, but the reverse function modifies the original one, so It works somewhat like C++ program would do. Could anyone explain me the difference?
PS: Sorry if it is a dummy question, I am totally new to the Golang and trying to understand the basics well.