In Go, you declare an array by specifying the type and length of the array.
In this example,
numbers
of type [5]int
and initialize it with some values.package main
import "fmt"
func main() {
numbers := [5]int{1, 2, 3, 4, 5}
fmt.Println("Array elements:", numbers)
}
Array elements: [1 2 3 4 5]
In this example,
names
of type [3]string
and initialize it with some values.package main
import "fmt"
func main() {
names := [3]string{"Alice", "Bob", "Charlie"}
fmt.Println("Array elements:", names)
}
Array elements: [Alice Bob Charlie]
In this tutorial, we learned How to Declare an Array in Go language with well detailed examples.