Go Arrays

Gayathri. B

Go Arrays

  • Arrays are used to store multiple values of the same type in a single variable, instead of declaring separate variables for each value.

Declare an Array

  • In Go, there are two ways to declare an array:

1. With the var keyword:

Syntax:
var array_name = [length]datatype{values} // here length is defined
                                    (or)
var array_name = [...]datatype{values} // here length is inferred

2. With the := sign:

Syntax:
array_name := [length]datatype{values} // here length is defined
                                   (or)
array_name := [...]datatype{values} // here length is inferred

Note: The length specifies the number of elements to store in the array. In Go, arrays have a fixed length. The length of the array is either defined by a number or is inferred (means that the compiler decides the length of the array, based on the number of values).

Array Example:

package main
import "fmt"
func main() {
  // Declare an array of 3 integers
  var numbers1 = [3]int{1, 2, 3}
  // Declare another array of 5 integers using shorthand syntax
  numbers2 := [5]int{4, 5, 6, 7, 8}

  fmt.Println("Array 1:", numbers1)
  fmt.Println("Array 2:", numbers2)
}

Output:

Array 1: [1 2 3]
Array 2: [4 5 6 7 8]

Example1:

package main
import "fmt"
func main() {
  // Declare array with inferred length using var keyword
  var arr1 = [...]int{1, 2, 3}
  // Declare array with inferred length using shorthand syntax
  arr2 := [...]int{4, 5, 6, 7, 8}
  // Print the arrays
  fmt.Println("arr1:", arr1)
  fmt.Println("arr2:", arr2)
}

Output:

arr1: [1 2 3]
arr2: [4 5 6 7 8]

Example2:

package main
import "fmt"
func main() {
  // Declare an array of strings with 4 elements
  cars := [4]string{"Volvo", "BMW", "Ford", "Mazda"}
  // Print the array
  fmt.Println(cars)
}

Output:

[Volvo BMW Ford Mazda]

Accessing Elements in an Array

  • To retrieve specific values from an array in Go, use the index number of the element. Indexing starts at 0, meaning the first element is at index 0, the second at index 1, and so on.

Example3:

The following example demonstrates how to access the first and third elements of an array named prices:

package main
import (
  "fmt"
)
func main() {
  prices := [3]int{10, 20, 30}
  fmt.Println("First price:", prices[0])
  fmt.Println("Third price:", prices[2])
}

Output:

First price: 10  
Third price: 30

Modifying Elements in an Array

  • In Go, you can update the value of a specific array element by using its index.

Example:

The following program demonstrates how to change the value of the third element in the prices array:

package main
import "fmt"
func main() {
  prices := [3]int{10, 20, 30}
  // Update the third element (index 2)
  prices[2] = 50
  fmt.Println(prices)
}

Output:

[10 20 50]

Array Initialization in Go

  • In Go, when an array or any of its elements is not explicitly initialized, the elements are automatically assigned their default values.

Tip:
  • The default value for int is 0.
  • The default value for string is "" (an empty string).

Example

package main
import ("fmt")
func main() {
  arr1 := [5]int{}             // All elements use default value (0)
  arr2 := [5]int{1, 2}         // First two elements set, rest use default value (0)
  arr3 := [5]int{1, 2, 3, 4, 5} // All elements initialized
  fmt.Println(arr1)
  fmt.Println(arr2)
  fmt.Println(arr3)
}

Output:

[0 0 0 0 0]
[1 2 0 0 0]
[1 2 3 4 5]

Initialize Only Specific Elements in an Array

  • In Go, you can initialize only certain elements of an array by specifying their index explicitly.

Example

The following example initializes only the second (index 1) and third (index 2) elements of a 5-element array:

package main
import ("fmt")
func main() {
  arr := [5]int{1: 10, 2: 40}
  fmt.Println(arr)
}

Output:

[0 10 40 0 0]

Example Explained

The following array has 5 elements:

arr1 := [5]int{1:10, 2:40}
  • 1:10 assigns the value 10 to index 1 (second element).
  • 2:40 assigns the value 40 to index 2 (third element).
  • All other elements are set to the default value of 0.

Finding the Length of an Array

  • You can use the len() function in Go to find the number of elements in an array.

Example:

package main
import ("fmt")
func main() {
  arr1 := [4]string{"Volvo", "BMW", "Ford", "Mazda"}
  arr2 := [...]int{1, 2, 3, 4, 5, 6}
  fmt.Println(len(arr1)) // Output: 4
  fmt.Println(len(arr2)) // Output: 6
}

Output:

4  
6
  • len(arr1) returns 4 because the array contains 4 strings.
  • len(arr2) returns 6 because the array is initialized with 6 integers.
Tags
Our website uses cookies to enhance your experience. Learn More
Accept !

GocourseAI

close
send