create an array with a length of type int in go

To create an array with a length of type integer in Golang, you can declare the array with the type of the elements it will contain and the length of the array in square brackets. Here's an example of how to create an array of type int with length 5:

main.go
var numbers [5]int
19 chars
2 lines

In this example, we declared an array of integers called "numbers" with a length of 5. You can access individual elements of the array by their index, starting from zero:

main.go
numbers[0] = 1
numbers[1] = 2
numbers[2] = 3
numbers[3] = 4
numbers[4] = 5
75 chars
6 lines

Alternatively, you can initialize the array when you declare it:

main.go
numbers := [5]int{1, 2, 3, 4, 5}
33 chars
2 lines

This is known as an array literal. In this case, we declared and initialized an array of integers called "numbers" with the values 1 through 5.

gistlibby LogSnag