Example: Arrays of Arrays, Arrays of Slices, Slices of Arrays and Slices of Slices
Using dots ... instead of length when declaring an array instructs the compiler to calculate the length by including three dots in place of length declaration
[:]
assigns all element value of array to slicearrayofarrays
is a multidimensional arrayExample
package main
import "fmt"
func main() {
println("Simple Array:")
var arrayint = [...]int{1, 2, 3, 4} //assign
fmt.Println(arrayint, "\n")
println("Simple Slice:")
var sliceint []int
sliceint = arrayint[:] //assign
fmt.Println(sliceint, "\n")
println("Array of arrays:")
var arrayofarrays [3][len(arrayint)]int
for i := range arrayofarrays { //assign
arrayofarrays[i] = arrayint
}
fmt.Println(arrayofarrays, "\n")
println("Array of slices:")
var arrayofslice [len(arrayofarrays)][]int
for i := range arrayofarrays { // assign
arrayofslice[i] = arrayofarrays[i][:]
}
fmt.Println(arrayofslice, "\n")
println("Slice of arrays:")
var sliceofarray [][len(arrayint)]int
sliceofarray = arrayofarrays[:]
fmt.Println(sliceofarray, "\n")
println("Slice of slices:")
var sliceofslices [][]int
sliceofslices = arrayofslice[:]
fmt.Println(sliceofslices, "\n")
}
Output
Simple Array:
[1 2 3 4]
Simple Slice:
[1 2 3 4]
Array of arrays:
[[1 2 3 4] [1 2 3 4] [1 2 3 4]]
Array of slices:
[[1 2 3 4] [1 2 3 4] [1 2 3 4]]
Slice of arrays:
[[1 2 3 4] [1 2 3 4] [1 2 3 4]]
Slice of slices:
[[1 2 3 4] [1 2 3 4] [1 2 3 4]]
Most Helpful This Week
Regular expression to validate the date format in "dd/mm/yyyy"
Regular expression to extract filename from given path in Golang
Golang HTTP GET request with parameters
How to check pointer or interface is nil?
How to convert Colorful PNG image to Gray-scale?
How to check if a string contains a white space in Golang?
Different ways to convert Byte Array into String
Convert specific UTC date time to PST, HST, MST and SGT
How to Convert string to float type in Go?
Encoding and Decoding using json.Marshal and json.Unmarshal