2月7日 11:06

How to Create Arrays in Golang?

In Go, arrays can be created by specifying the element type and array length. The length of an array is fixed and cannot be changed once defined. Here is a specific example of how to create an array:

go
var myArray [5]int // Create an integer array of length 5 with all elements initialized to zero // Assign values to the array myArray[0] = 10 myArray[1] = 20 myArray[2] = 30 myArray[3] = 40 myArray[4] = 50

You can also initialize the array at the time of declaration:

go
myArray := [5]int{10, 20, 30, 40, 50}

Both methods can be used to create and initialize an array in Go.

标签:Golang