Go – Array Literal

In Go, array literals provide a concise way to declare and initialize arrays in a single statement. They allow you to define arrays with specific values without needing separate declarations and assignments. This makes code cleaner and easier to read.

In this tutorial, we will explore how to use array literals in Go with practical examples and detailed explanations.


Syntax for Array Literals

The syntax for an array literal in Go is:

</>
Copy
var arrayName = [length]elementType{value1, value2, ..., valueN}

Here:

  • arrayName: The name of the array.
  • length: The fixed size of the array.
  • elementType: The type of elements in the array (e.g., int, float64, string, etc.).
  • value1, value2, ..., valueN: The values assigned to the array elements.

Examples of Array Literals

1 Integer Array Literal

Here’s how to declare and initialize an integer array using a literal:

</>
Copy
package main

import "fmt"

func main() {
    // Declare and initialize an integer array
    numbers := [5]int{10, 20, 30, 40, 50}

    // Print the array
    fmt.Println("Integer Array:", numbers)
}

Explanation

  1. Declare Array: The array numbers is declared with a size of 5 and type int.
  2. Initialize with Literal: The values {10, 20, 30, 40, 50} are assigned to the array elements using a literal.
  3. Print Array: The array is printed using fmt.Println.

Output


2 String Array Literal

Here’s how to declare and initialize a string array using a literal:

</>
Copy
package main

import "fmt"

func main() {
    // Declare and initialize a string array
    fruits := [3]string{"Apple", "Banana", "Cherry"}

    // Print the array
    fmt.Println("String Array:", fruits)
}

Explanation

  1. Declare Array: The array fruits is declared with a size of 3 and type string.
  2. Initialize with Literal: The values {"Apple", "Banana", "Cherry"} are assigned to the array elements using a literal.
  3. Print Array: The array is printed using fmt.Println.

Output


3 Float Array Literal

Here’s how to declare and initialize a float array using a literal:

</>
Copy
package main

import "fmt"

func main() {
    // Declare and initialize a float array
    temperatures := [4]float64{36.5, 37.8, 39.2, 40.1}

    // Print the array
    fmt.Println("Float Array:", temperatures)
}

Explanation

  1. Declare Array: The array temperatures is declared with a size of 4 and type float64.
  2. Initialize with Literal: The values {36.5, 37.8, 39.2, 40.1} are assigned to the array elements using a literal.
  3. Print Array: The array is printed using fmt.Println.

Output


Points to Remember

  • Fixed Size: Arrays declared using literals have a fixed size defined during initialization.
  • Type Inference: If the type is not explicitly mentioned, Go can infer the type from the literal values.
  • Default Values: Uninitialized elements in an array default to the zero value of the type.
  • Readability: Using literals improves code readability by reducing the need for multiple lines of initialization.