Fundamentals 7 min read

Master Go Arrays: Definitions, Initialization, and Traversal Explained

This article introduces Go arrays, covering their definition as contiguous fixed-length collections of identical types, demonstrates various declaration syntaxes, shows how to initialize arrays with explicit values or automatic length inference, explains traversal techniques using for loops and range, and explores multidimensional arrays with practical code examples.

Python Crawling & Data Mining
Python Crawling & Data Mining
Python Crawling & Data Mining
Master Go Arrays: Definitions, Initialization, and Traversal Explained

Preface

Hey everyone, I'm 星期八 and we continue learning Go arrays.

What is an array

My summary: an array is a variable that points to a contiguous block of memory with a fixed length and elements of the same type.

How to define an array

var variableName [size]elementType

Example:

package main

func main() {
    // Declare a name_list array of length 100 that holds strings
    var name_list [100]string
}

Note:

var variableName [size]elementType is equivalent to var variableName variableType
So var name1 [3]int != var name2 [4]int because their types differ and cannot be assigned directly.

Array initialization

package main

import "fmt"

func main() {
    // Method 1: declare without assigning
    // var name_list [10]int
    // fmt.Println(name_list) // [0 0 0 0 0 0 0 0 0 0]
    // Method 2: partially assign
    // var name_list [10]int = [10]int{1, 3}
    // fmt.Println(name_list) // [1 3 0 0 0 0 0 0 0 0]
    // Method 3: full assignment with type inference
    // var name_list = [3]int{1, 6, 10}
    // fmt.Println(name_list) // [1 6 10]
    // Method 4: let compiler infer length
    // var name_list = [...]int{1, 2, 4, 5, 19}
    // fmt.Println(name_list) // [1 2 4 5 19]
    // Method 5: indexed assignment (rare)
    var name_list = [...]int{1: 66, 4: 11} // index 1 = 66, index 4 = 11
    fmt.Println(name_list) // [0 66 0 0 11]
}

Array traversal

package main

import "fmt"

func main() {
    var name_list = [...]string{"张三", "李四", "王五", "小刘"}
    // Method 1: classic for loop (commented)
    // for i := 0; i < len(name_list); i++ {
    //     fmt.Println(name_list[i])
    // }
    // Method 2: for range
    for index, name := range name_list {
        fmt.Println(index, name)
    }
}

Multidimensional arrays

Two‑dimensional arrays

Usually a two‑dimensional array is sufficient; three dimensions are the practical limit.

Define a two‑dimensional array

package main

import "fmt"

func main() {
    // Define a 3x2 array
    var student_list = [3][2]string{{"张三", "李四"}, {"王五", "小刘"}, {"小七", "王八"}}
    fmt.Println(student_list)
}

Iterate a two‑dimensional array

Two nested loops are needed.

package main

import "fmt"

func main() {
    var student_list = [3][2]string{{"张三", "李四"}, {"王五", "小刘"}, {"小七", "王八"}}
    // Method 1: classic for loops (commented)
    // for i := 0; i < len(student_list); i++ {
    //     for j := 0; j < len(student_list[i]); j++ {
    //         fmt.Println(student_list[i][j])
    //     }
    // }
    // Method 2: for range
    for _, row := range student_list {
        for _, col := range row {
            fmt.Println(col)
        }
    }
}

Can length be inferred for multidimensional arrays?

Attempting to use [...] for both dimensions causes a compilation error:

Using inference only on the first dimension works:

package main

import "fmt"

func main() {
    var student_list = [...][2]string{{"张三", "李四"}, {"王五", "小刘"}, {"小七", "王八"}}
    fmt.Println(student_list)
}

Note: Length inference is allowed for the first dimension.

Summary

We have covered Go arrays, including their definition, declaration, initialization, traversal, and usage of multidimensional arrays.

If you have questions, leave a comment; the author will respond promptly.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

GoInitializationArraysTraversalMultidimensional
Python Crawling & Data Mining
Written by

Python Crawling & Data Mining

Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.