In Go, arrays are designed to store multiple values of the same type within a single variable. This feature allows developers to work efficiently without the need for creating multiple variables for related data.
How to Declare Arrays
Arrays in Go can be declared using two primary methods:
- Using the
var
keyword:var arrayName = [length]dataType{values} // Fixed length var arrayName = [...]dataType{values} // Length inferred
- Using shorthand with
:=
:arrayName := [length]dataType{values} // Fixed length arrayName := [...]dataType{values} // Length inferred
Note: The length of an array is always fixed in Go. It can be explicitly stated as a number or inferred automatically by the compiler based on the number of values.
Examples of Arrays in Go
Example 1: Declaring Arrays with Fixed Length
package main
import ("fmt")
func main() {
var motorcycleModels = [3]string{"Ducati", "Yamaha", "Honda"}
brands := [4]string{"Suzuki", "Honda", "Yamaha", "Ducati"}
fmt.Println(motorcycleModels)
fmt.Println(brands)
}
Output:
[Ducati Yamaha Honda]
[Suzuki Honda Yamaha Ducati]
Example 2: Declaring Arrays with Inferred Length
package main
import ("fmt")
func main() {
var motorcycleModels = [...]string{"Ducati", "Yamaha", "Honda"}
brands := [...]string{"Suzuki", "Honda", "Yamaha", "Ducati"}
fmt.Println(motorcycleModels)
fmt.Println(brands)
}
Output:
[Ducati Yamaha Honda]
[Suzuki Honda Yamaha Ducati]
Example 3: Array of Integers
package main
import ("fmt")
func main() {
var motorcycleYears = [5]int{2019, 2020, 2021, 2022, 2023}
fmt.Println(motorcycleYears)
}
Output:
[2019 2020 2021 2022 2023]
Accessing Array Elements
To access a specific element in an array, simply use its index. Array indexing begins at 0
.
Example:
package main
import ("fmt")
func main() {
prices := [3]int{25000, 30000, 35000}
fmt.Println(prices[0]) // Access first element
fmt.Println(prices[2]) // Access third element
}
Output:
25000
35000
Modifying Array Elements
It is also possible to change an element within an array by referring to its index.
Example:
package main
import ("fmt")
func main() {
prices := [3]int{25000, 30000, 35000}
prices[2] = 40000 // Modify the third element
fmt.Println(prices)
}
Output:
[25000 30000 40000]
Array Initialization Defaults
When an array or one of its elements is not explicitly initialized, Go assigns a default value based on the data type. For example, the default for integers is 0
, and for strings, it is ""
.
Example:
package main
import ("fmt")
func main() {
brands := [5]string{} // Default initialization
years := [5]int{2020, 2021} // Partially initialized
models := [5]string{"Ducati", "Yamaha", "Honda", "Suzuki", "BMW"} // Fully initialized
fmt.Println(brands)
fmt.Println(years)
fmt.Println(models)
}
Output:
["" "" "" "" ""]
[2020 2021 0 0 0]
[Ducati Yamaha Honda Suzuki BMW]
Initializing Specific Elements Only
Go allows you to initialize specific elements in an array by specifying index-value pairs.
Example:
package main
import ("fmt")
func main() {
years := [5]int{2:2018, 4:2020} // Initialize specific elements
fmt.Println(years)
}
Output:
[0 0 2018 0 2020]
Finding the Array Length
The len()
function is available to calculate the length of an array.
Example:
package main
import ("fmt")
func main() {
motorcycleBrands := [4]string{"Ducati", "Yamaha", "Honda", "Suzuki"}
releaseYears := [...]int{2018, 2019, 2020, 2021, 2022}
fmt.Println(len(motorcycleBrands))
fmt.Println(len(releaseYears))
}
Output:
4
5