array in golang
// Sum function takes a pointer of array with 3 float numbers. if you define `a *[4]float64` then you get type error
func Sum(a *[3]float64) (sum float64) {
for _, v := range *a {
sum += v
}
return
}
array := [...]float64{7.0, 8.5, 9.1}
x := Sum(&array) // Note the explicit address-of operator
/*
## Array in Golang
1. There are major differences between the ways arrays work in
Go and C. In Go:
- Arrays are values. Assigning one array to another copies
all the elements.
- In particular, if you pass an array to a function, it will
receive a copy of the array, not a pointer to it.
- The size of an array is part of its type. The types [10]int
and [20]int are distinct.
2. The value property can be useful but also expensive; if you
want C-like behavior and efficiency, you can pass a pointer
to the array.
*/