How To Use Arrays and Slices in Google Go
Array
An array is a collection of like objects. In Google Go programming, arrays are declared as,
var arrayOfInt [10]int
The length is part of the array’s type and must be a constant expression that evaluates to a non-negative integer value. The length of array a can be discovered using the built-in function len(a). The elements can be indexed by integer indices 0 through the len(a)-1. If the array is indexed beyond len(a)-1, then we get an out of bounds error.
In C, arrayOfInt would be usable as a pointer to int. In Go, since arrays are values, we have to use a pointer to an array. What this means is that in Google Go,
var pArray *int = &arrayOfInt //pArray is a pointer to an integer. var pArray *[10]int = &arrayOfInt //pArray is a pointer to an array of 10 integers.
In the declaration of the pointer to an array, the size of the array is mandatory. When assigning the address of an array to a pointer variable, the & referencing is mandatory.
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 value property can be useful but also expensive; if you want C-like behavior and efficiency, you can pass a pointer to the array.
package main
import "fmt"
func arrayLen (pArray *[5]int) int{
return len(pArray)
}
func main(){
var arrayOfInt [5]int
arrayOfInt[0] = 0
arrayOfInt[1] = 1
arrayOfInt[2] = 2
arrayOfInt[3] = 3
arrayOfInt[4] = 4
fmt.Println("Array Length = ", arrayLen(&arrayOfInt))
}
If you are creating a regular array but want the compiler to count the elements for you, use … as the array size:
s := sum(&[...]int{1,2,3})
A type followed by a brace-bounded expression—is a constructor for a value, in this case an array of 3 ints.
Slice
Slices wrap arrays to give a more general, powerful, and convenient interface to sequences of data. Slices can be considered as a sub-section of an array. Slices are reference types, which means that if you assign one slice to another, both refer to the same underlying array. For instance, if a function takes a slice argument, changes it makes to the elements of the slice will be visible to the caller, analogous to passing a pointer to the underlying array.
We can declare a slice variable, by assigning a pointer to any array with the same element type,
var arrayOfInt [5]int var slice []int = &arrayOfInt
or by a slice expression of the form a[low : high], representing the subarray indexed by low through high-1.
var arrayOfInt [5]int slice = arrayOfInt[1:3]
Slices look a lot like arrays but have no explicit size ([] vs. [10]) and they reference a segment of an underlying, often anonymous, regular array. Multiple slices can share data if they represent pieces of the same array; multiple arrays can never share data.
When passing an array to a function, you almost always want to declare the formal parameter to be a slice. When you call the function, take the address of the array and Go will create (efficiently) a slice reference and pass that.
Using slices one can write the earlier program as:
package main
import "fmt"
func arrayLen (slice []int) int{
return len(slice)
}
func main(){
var arrayOfInt [5]int
arrayOfInt[0] = 0
arrayOfInt[1] = 1
arrayOfInt[2] = 2
arrayOfInt[3] = 3
arrayOfInt[4] = 4
fmt.Println("Array Length = ", arrayLen(&arrayOfInt))
}
We pass the pointer to arrayLen() by (implicitly) promoting it to a slice.
Like arrays, slices are indexable and have a length. The length of a slice s can be discovered by the built-in function len(s); unlike with arrays it may change during execution. The elements can be addressed by integer indices 0 through len(s)-1. The slice index of a given element may be less than the index of the same element in the underlying array.


Jacob 4:44 am on August 1, 2010 Permalink |
I have been reading your tutorials Google Go. How would I prompt for user input and put it into a variable? I cannot seem to find how to do this. Thanks in advance.
Kevin 8:22 am on August 1, 2010 Permalink |
Hi Jacob,
Thanks for reading Complete Coding.
The “fmt” package available in Google Go implements formatted I/O with functions analogous to C’s printf and scanf.
So if you want to prompt for user input and put it into a variable you can do something like,
package main
import “fmt”
func main(){
var a int
fmt.Print(“Read a:”)
fmt.Scan(&a)
fmt.Println(” a =”, a)
}
There are many more functions available in the “fmt” package and you can find more information at http://golang.org/pkg/fmt/
Keep reading Complete Coding for more tutorials on Google Go programming.