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
How To Make an HTTP Server in Golang?
How to read/write from/to file in Golang?
How to get Dimensions of an image type jpg jpeg png or gif ?
Convert Int data type to Int16 Int32 Int64
How to read names of all files and folders in current directory?
Simple function with parameters in Golang
How to trim leading and trailing white spaces of a string in Golang?
How to use wildcard or a variable in our URL for complex routing?
Replace first occurrence of string using Regexp
Example to handle GET and POST request in Golang