Today, we are going to learn about arrays and slices in Go.
Today, we are going to learn about arrays and slices in Go.
Arrays
An array is a collection of the same type of elements. Mixing different types of elements is not allowed in Go. For Example, [1,2,3,4,7,8,90] is valid however, [1,2,’string’,4.5,7] is not valid.
Declaration of an array is: [number of elements] Data Type
package main
import (
"fmt"
)
func main() {
var arr [5]int //int array with length 5
fmt.Println(arr)
}
Here, all array items are immediately allocated the array type's zero value. Because an is an integer array in this example, all of its elements are assigned to 0, the zero value of int.
Output: [0, 0, 0, 0, 0]
Note: The index of an array starts from 0 to length - 1
Let’s understand how to assign in array with the following example:
package main
import (
"fmt"
)
func main() {
var arr [5]int //int array with length 3
arr[0] = 12 // array index starts at 0
arr[1] = 78
arr[2] = 50
fmt.Println(arr)
}
Here, we only initiated the 3 elements in the array and the rest 2 are still 0.
Output: [12 78 50 0 0]
Initialize the array when declared
We can also initialize the array when it is declared this is also known as Short hand declaration
For example:
package main
import (
"fmt"
)
func main() {
a := [3]int{12, 78, 50} // short hand declaration to create array
fmt.Println(a)
}
Output: [12, 78, 50]
We can also ignore the length of the array in the declaration and still initialize the array as the compiler will decide the length of the array
Like a := [...]int{12, 78, 50}
Length of an array
We can get the length of an array with the help of len function which takes the array's name as a parameter.
package main
import "fmt"
func main() {
arr := [...]int{67, 89, 21, 78}
fmt.Println("length of arr is", len(arr))
}
Output: length of arr is 4
Iterating over array
Here, For loop is used to iterate over the array elements.
For example:
package main
import "fmt"
func main() {
arr := [...]float64{67.7, 89.8, 21.7, 7.8, 77.0}
for i := 0; i < len(arr); i++ { //looping from 0 till the length of the array
fmt.Printf("%d th element of arr is %f\n", i, arr[i])
}
}
Output:
0 th element of arr is 67.700000
1 th element of arr is 89.800000
2 th element of arr is 21.700000
3 th element of arr is 7.800000
4 th element of arr is 77.000000
Go also provides a better function to iterate over the elements of an array which is range function. The range version of the for loop in Go is a more efficient and succinct approach to iterating over an array. range returns the index as well as the value at that index.
Syntax:
for i, v := range a { //range returns both the index and value
fmt.Printf("%d the element of a is %f\n", i, v)
}
Multidimensional Arrays
So far we have learned about one-dimensional array. Now, we are going to learn about two-dimensional arrays and more.
For Example:
package main
import (
"fmt"
)
func printarray(a [3][2]string) {
for _, v1 := range a {
for _, v2 := range v1 {
fmt.Printf("%s ", v2)
}
fmt.Printf("\n")
}
}
func main() {
a := [3][2]string{
{"lion", "cub"},
{"cat", "kitten"},
{"pigeon", "squabs"}, //this comma is necessary
}
printarrlion cub
cat kitten
pigeon squabsay(a)
}
Output:
lion cub
cat kitten
pigeon squabs
Slices
A slice is an array's handy, versatile, and powerful wrapper. Slices do not have their own data. They are just pointers to pre-existing arrays.
It’s syntax is [ ]DataType
The syntax a[start : end] creates a slice from array a starting from index start to index end - 1.
For Example,
package main
import (
"fmt"
)
func main() {
a := [5]int{1, 2, 3, 4, 5}
var b []int = a[1:4] //creates a slice from a[1] to a[3]
fmt.Println(b)
}
Output: [2 3 4]
Modifying Slices
A slice has no independent data ownership. Simply said, it is an illustration of the underlying array. The underlying array will reflect any changes made to the slice.
For Example:
package main
import (
"fmt"
)
func main() {
arr := [...]int{57, 89, 90, 82, 100, 78, 67, 69, 59}
slice := arr[2:5]
fmt.Println("array before", arr)
for i := range slice {
slice[i]++
}
fmt.Println("array after", arr)
}
Output:
array before [57 89 90 82 100 78 67 69 59]
array after [57 89 91 83 101 78 67 69 59]
Add a comment: