To initialize an array in Go, you can use array literals or the make() function. In this tutorial, we will go through examples on how to use array literals or the make() function to create integer arrays, string arrays, etc.
In this example,
numbers
using array literals with integer values.package main
import "fmt"
func main() {
numbers := [5]int{1, 2, 3, 4, 5}
fmt.Println(numbers)
}
[1 2 3 4 5]
In this example,
fruits
using the make()
function with string values.package main
import "fmt"
func main() {
fruits := make([]string, 3)
fruits[0] = "Apple"
fruits[1] = "Banana"
fruits[2] = "Orange"
fmt.Println(fruits)
}
[Apple Banana Orange]
In this tutorial, we learned How to Initialize an Array in Go language with well detailed examples.