Arrays
Arrays are container types in golang with a specific type, in a numbered sequence
package main
import "fmt"
func main() {
// declare an empty int array with size 5
var arr [5]int
fmt.Println("empty array:", arr)
// set the element at the 4th index = 23
arr[4] = 100
fmt.Println("set:", arr)
// we can print individual array items using the index
fmt.Println("get:", arr[4])
// the len() function allows us to print the length of the array
fmt.Println("len:", len(a))
// arrays can also be intialized with some values
arr2 := [5]int{1, 2, 3, 4, 5}
fmt.Println("with vals:", arr2)
// two dimentional arrays
var arr_2d [5][7]int
for i := 0; i < 5; i++ {
for j := 0; j < 7; j++ {
arr_2d[i][j] = i + j
}
}
fmt.Println("2D Array: ", arr_2d)
}Looping through Arrays
Last updated