ELEVATE YOUR BUSINESS WITH

Limitless customization options & Elementor compatibility let anyone create a beautiful website with Valiance.

Arrays in GoLang

SELECT * FROM `itio_tutorial_master` WHERE `tutorial_menu`='1' AND `tutorial_submenu`='17' AND `tutorial_status`=1 LIMIT 1

Arrays in GoLang

In Go, arrays are fixed-size collections of elements of the same type. They provide a way to store multiple values in a single variable. Below are the key concepts and usage examples of arrays in Go:


Syntax

var arrayName [size]Type

  • size is the number of elements in the array.
  • Type specifies the type of elements in the array.

Examples

1. Declaring and Initializing an Array

var numbers [5]int // An array of 5 integers with default value 0
fmt.Println(numbers) // Output: [0 0 0 0 0]

// Declaring and initializing in one line
days := [7]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"}
fmt.Println(days) // Output: [Monday Tuesday Wednesday Thursday Friday Saturday Sunday]

// Letting Go infer the size
colors := [...]string{"Red", "Green", "Blue"}
fmt.Println(colors) // Output: [Red Green Blue]

2. Accessing and Modifying Elements

var arr [3]int
arr[0] = 10
arr[1] = 20
arr[2] = 30
fmt.Println(arr) // Output: [10 20 30]

fmt.Println(arr[1]) // Output: 20


3. Iterating Over an Array

Using for loop:

numbers := [5]int{1, 2, 3, 4, 5}
for i := 0; i < len(numbers); i++ {
fmt.Println(numbers[i])
}

Using for range:

for index, value := range numbers {
fmt.Printf("Index: %d, Value: %d\n", index, value)
}

4. Multi-Dimensional Arrays

var matrix [2][3]int
matrix[0][0] = 1
matrix[0][1] = 2
matrix[0][2] = 3
matrix[1][0] = 4
matrix[1][1] = 5
matrix[1][2] = 6
fmt.Println(matrix)
// Output: [[1 2 3] [4 5 6]]

5. Passing Arrays to Functions

Arrays in Go are passed by value (a copy of the array is passed).

func modifyArray(arr [3]int) {
arr[0] = 100
}

func main() {
nums := [3]int{1, 2, 3}
modifyArray(nums)
fmt.Println(nums) // Output: [1 2 3]
}

If you want to modify the original array, pass a pointer to it:

func modifyArray(arr *[3]int) {
arr[0] = 100
}

func main() {
nums := [3]int{1, 2, 3}
modifyArray(&nums)
fmt.Println(nums) // Output: [100 2 3]
}


6. Comparing Arrays

Arrays can be compared using the == operator if they are of the same type and size.

a := [3]int{1, 2, 3}  
b := [3]int{1, 2, 3}  
c := [3]int{4, 5, 6}    
fmt.Println(a == b) // Output: true  
fmt.Println(a == c) // Output: false


Key Points

  • Arrays in Go have a fixed size and cannot be resized.
  • For dynamic sizes, use slices, which are more flexible and commonly used.
  • Arrays are value types, meaning modifications to an array in a function do not affect the original unless passed by reference.

Let me know if you want more details about slices or specific array operations!


Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql