Learn how to create an empty array in Golang using different methods. Understand the difference between arrays and slices with practical examples and best practices.
In Golang (Go), arrays are one of the most fundamental data structures in any programming language, and Go (or Golang) is no exception. Understanding how to create and manage arrays efficiently is key to writing performant Go code. In this post, we’ll explore several ways to create an empty array in Go, discuss how arrays differ from slices, and review some best practices for using them.
Table of Contents
What Is an Array in Golang?
An array in Golang is a fixed-size sequence of elements of the same type. Once created, its length cannot change. This immutability makes arrays perfect for predictable, performance-sensitive use cases where the number of items is known in advance.
The syntax for declaring an array looks like this:
var arr [5]int
This creates an array of five integers, with each element automatically initialized to its zero value “0” for integers.
Creating an Empty Array in Go
To create an empty array (that is, an array with zero elements), you can declare it with a length of zero:
var emptyArray [0]int
Here, emptyArray is an array of integers with a length of zero. While you can technically create it, arrays of zero length are rarely used directly since they can’t store any elements.
You can also initialize an empty array using the array literal syntax:
emptyArray := [0]int{}
This form is more concise and idiomatic in Go.
Difference Between Arrays and Slices
It’s important to differentiate between arrays and slices in Go because slices are more common in real-world code.
| Feature | Array | Slice |
|---|---|---|
| Size | Fixed | Dynamic |
| Declaration | var arr [5]int | var slice []int |
| Capacity | Cannot change | Grows or shrinks |
| Passing to Functions | Passed by value | Passed by reference |
If your goal is to work with a collection of elements that may grow or shrink, you’ll likely want a slice instead of an array.
Creating an Empty Slice (Preferred in Most Cases)
Most Go developers use slices instead of arrays, especially when dealing with dynamic data. To create an empty slice:
var emptySlice []int
or
emptySlice := []int{}
Both examples produce a nil or zero-length slice that you can later append elements to:
emptySlice = append(emptySlice, 10)
fmt.Println(emptySlice) // Output: [10]
Empty Array vs Empty Slice
In Go, arrays have a fixed size, while slices are more flexible and dynamically sized.
If you’re looking for a “resizable empty collection,” you should use a slice instead of an array.
Example – Empty Slice:
var s []int
fmt.Println(s) // []
fmt.Println(len(s)) // 0
fmt.Println(cap(s)) // 0
Or, initialize it explicitly:
s := []int{}
Arrays are best when:
- You need fixed-size collections.
- You care about performance or memory predictability.
Slices are best when:
- You need dynamic, resizable data structures.
- You’re working with collections whose size changes at runtime.
Practical Example: Arrays and Slices in Action
Here’s a complete example showing both arrays and slices:
package main
import "fmt"
func main() {
// Empty array of 4 integers
var arr [4]int
fmt.Println("Empty Array:", arr)
// Empty slice
s := []int{}
fmt.Println("Empty Slice:", s)
// Append to slice
s = append(s, 10, 20, 30)
fmt.Println("Updated Slice:", s)
}
Output:
Empty Array: [0 0 0 0]
Empty Slice: []
Updated Slice: [10 20 30]
Best Practices
- Use arrays when the size is known and fixed.
- Use slices for variable-length data.
- Avoid using zero-length arrays unless they serve a specific purpose (like compliance with a specific function signature or interface).
Common Mistakes to Avoid
🚫 Trying to resize an array:
You can’t append to an array. Use a slice instead.
🚫 Confusing zero-length arrays with nil slices:
An empty array is not nil. Its length is defined as 0, but it occupies memory.
✅ Best practice:
If you need to build collections dynamically, always start with a slice.
Conclusion
Creating an empty array in Golang is simple, but understanding its behavior is essential for writing efficient Go programs. Arrays provide predictable performance and memory layout, while slices offer flexibility.
- Use arrays when you need fixed-size, low-level storage.
- Use slices when working with variable-length data in production.
By mastering both, you’ll have a stronger grasp of Go’s memory model and data handling.