Array in Go
Go: Array
Array in Go, is a numbered sequence of elements of a specific length.
Here we will discuss about array declaration, array initializatttion and various use cases of Array in Golang.
1. Array Declaration in Go
var a [10]int
fmt.Println("arr:", a)
// arr: [0 0 0 0 0 0 0 0 0 0]
2. Array initializatttion in Go
var a := [5]int{1, 2, 3, 4, 5}
fmt.Println("arr:", a)
// arr: [1, 2, 3, 4, 5]
// change individual values
a[4] = 10
fmt.Println("arr:", a)
// arr: [1, 2, 3, 4, 10]
3. 2D Array in Go
var twoDArr [2][3]int
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
twoDArr[i][j] = i + j
}
}
fmt.Println("2d: ", twoD)
//2d: [[0 1 2] [1 2 3]]
Previous Article
Next Article
Golang Tutorials
- Hello World
- Operators in Go
- Declarations in Go
- Values in Go
- Variables in Go
- For in Go
- If/Else in Go
- Switch in Go
- Arrays in Go
- Slices in Go
- Maps in Go
- Range in Go
- Functions in Go
- Closures in Go
- Recursion in Go
- Pointers in Go
- Strings and Runes in Go
- Structs in Go
- Methods in Go
- Interfaces in Go
- Generics in Go
- Errors in Go
- Goroutines in Go
- Channels in Go
- Select in Go
- Timeouts in Go
- Timers in Go
- Worker Pools in Go
- WaitGroups in Go
- Mutexes in Go
- Sorting in Go
- Panic in Go
- Defer in Go
- Recover in Go
- JSON in Go
- XML in Go
- Time in Go
- Epoch in Go
- Time Formatting in Go
- Random Numbers in Go
- Number Parsing in Go
- URL Parsing in Go
- SHA256 Hashes in Go
- Base64 Encoding in Go
- Reading Files in Go
- Writing Files in Go
- File Paths in Go
- Directories in Go
- Testing and Benchmarking in Go
- Command-Line Arguments in Go
- Command-Line Flags in Go
- Command-Line Subcommands in Go
- Environment Variables in Go
- HTTP Client in Go
- HTTP Server in Go
- Context in Go
- Signals in Go