How to Create an Array in Go

Hey! If you love Go and building Go apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Want to learn how to build better Go applications faster and easier? You can.

Check out my course on the Go Standard Library. You can check it out now for free.


Arrays are a fundamental data structure in programming that allow you to store a fixed-size sequence of elements of the same type. In Golang, arrays are a basic data structure that can be used to store a collection of elements. In this tutorial, we’ll cover everything you need to know to create an array in Golang and use it in your programs.

Creating an Array in Golang

To create an array in Golang, you first need to specify the type of the elements it will hold, and then specify its size. Here’s an example of how to create an array that holds 5 integers:

var myArray [5]int

In this example, we’re creating an array named “myArray” that holds 5 integers. We’ve specified the size of the array using the number in brackets (5), and the type of the elements using the “int” keyword.

Initializing an Array in Golang

Once you’ve created an array, you can initialize its elements with values. Here’s an example of how to initialize an array with values:

var myArray = [5]int{1, 2, 3, 4, 5}

In this example, we’re creating an array named “myArray” that holds 5 integers, and initializing its elements with the values 1, 2, 3, 4, and 5.

Accessing Elements of an Array in Golang

To access elements of an array in Golang, you can use the index of the element you want to access. Here’s an example of how to access the first element of an array:

var myArray = [5]int{1, 2, 3, 4, 5}
fmt.Println(myArray[0])

In this example, we’re accessing the first element of the “myArray” array using its index (0), and printing its value using the “fmt.Println” function.

Looping Over an Array in Golang

To loop over an array in Golang, you can use a “for” loop and the “len” function to iterate over each element. Here’s an example of how to loop over an array and print each element:

var myArray = [5]int{1, 2, 3, 4, 5}

for i := 0; i < len(myArray); i++ {
    fmt.Println(myArray[i])
}

In this example, we’re using a “for” loop to iterate over each element of the “myArray” array, and printing its value using the “fmt.Println” function.

Why Use Arrays in Golang?

Arrays are useful in Golang for several reasons, including:

  1. Storing a collection of elements of the same type.

  2. Easy to declare and initialize.

  3. Fast to access elements, as the index is used to access elements directly.

  4. Can be used in combination with other data structures to build complex algorithms.

Conclusion

In this tutorial, we’ve covered everything you need to know to create and use arrays in Golang. We’ve learned how to create an array, initialize its elements, access its elements using indexes, and loop over its elements. Additionally, we’ve explored why arrays are useful in Golang, and how they can be used in combination with other data structures to build complex algorithms. By using arrays in your programs, you can efficiently store and access collections of data, making your programs more powerful and flexible.


Questions or comments? Reach out to me


Learn how to leverage the Go Standard Library like a PRO.

I just created a new course, The Go Standard Library, check it out!