Arrays in Golang
A Beginners guide to arrays in golang.
What is an array?
An array is a collection of elements of a specified length and type.
- Quick Note: Most go programmers use slices instead of arrays when coding in golang.
Key Points:
There are two ways of declaring an array in golang:
- Declare and initialize : DaysOfTheWeek := [7]string{“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”}
- Declare without giving the initial values: var TestScores [5]int (values initially default to 0 for integers)
Getting a value:
DaysOfTheWeek[0] // “Sunday”
Setting a value:
TestScores[0] = 100 // sets index 0 to the value of 100
Get the length of an Array:
len(DayOfTheWeek) //7