In this blog we will see the difference between array and slices in golang . In Go, an array and a slice are both fundamental data structures for storing collections of elements. However, they have some key differences in terms of their declaration, behavior, and usage. Here’s a breakdown of the differences between arrays and slices in Go:

- Declaration and Size:
- Array: When declaring an array, you specify its size, and it’s a fixed-length collection of elements. The size is part of the array’s type, so
[5]int
and[10]int
are distinct types in Go.
- Slice: A slice, on the other hand, is a dynamically-sized, flexible view into an underlying array. It doesn’t have a fixed size specified in its type declaration. Slices are declared using brackets
[]
, such as[]int
.
- Array: When declaring an array, you specify its size, and it’s a fixed-length collection of elements. The size is part of the array’s type, so
- Length and Capacity:
- Array: The compiler determines the length of an array at compile-time, and it remains unchanged during runtime. It is a fixed value.
- Slice: Slices have a length and capacity. The length represents the number of elements currently present in the slice, while the capacity is the maximum number of elements it can hold without resizing the underlying array. You can dynamically change the capacity of a slice.
- Passing and Assignment:
- Array: When passing an array to a function or assigning it to a new variable, a complete copy of the array is created. Changes made in the function or the new variable do not affect the original array.
- Slice: When working with slices, we pass it by reference, making them lightweight and efficient.. Modifying a slice inside a function or assigning it to another variable will affect the original slice since they share the same underlying array.
- Resizing:
- Array: As mentioned earlier, arrays have a fixed size determined at compile-time. To accommodate more elements, you need to create a new array with a larger size and copy the existing elements.
- Slice: Slices are designed for dynamic resizing. You can use the built-in
append()
function to add elements to a slice. If the slice exceeds its capacity during the append operation, the system allocates a new, larger array and copies the elements to the new array.
//array example
func main() {
// Declaring an array of integers with a fixed size of 5
var myArray [5]int
// Assigning values to the array elements
myArray[0] = 10
myArray[1] = 20
myArray[2] = 30
myArray[3] = 40
myArray[4] = 50
// Accessing and printing array elements
fmt.Println(myArray[0]) // Output: 10
fmt.Println(myArray[2]) // Output: 30
fmt.Println(myArray) // Output: [10 20 30 40 50]
}
// slice example
func main() {
// Declaring a slice of integers
var mySlice []int
// Appending elements to the slice
mySlice = append(mySlice, 10)
mySlice = append(mySlice, 20)
mySlice = append(mySlice, 30)
mySlice = append(mySlice, 40)
mySlice = append(mySlice, 50)
// Accessing and printing slice elements
fmt.Println(mySlice[0]) // Output: 10
fmt.Println(mySlice[2]) // Output: 30
fmt.Println(mySlice) // Output: [10 20 30 40 50]
}
In summary, we can conclude that arrays represent fixed-size collections of elements, whereas slices provide dynamic views into arrays that allow for resizing and offer greater flexibility in working with data. Developers often prefer using slices in Go due to their flexible nature and the availability of convenient operations.
That’s all for more such blogs explore our blogs page https://blog.nashtechglobal.com/.