Back to all posts
programming golang go

Go Arrays

0 min read (108 words)

Arrays have size - a fixed size, and cannot be resized (length is part of its type).

How to declare:

[size]type{}

To create an empty array:

var a [2]string

an array of 2 values of type string.

Can not mix types

func main() {
    var a [2]int // if need just empty array

    b := [2]string{"one"} // with some elements in the array
    b[1] = "two"

    a[0] = 0
    a[1] = 1
    fmt.Printf("The whole array: %v, %vn", a, b)
    fmt.Printf("The 1st in array: %v, %vn", a[0], b[0])
    fmt.Printf("The 2nd in array: %v, %vn", a[1], b[1])
    fmt.Printf("Array type: %T, %Tn", a, b)
    fmt.Printf("Array lengh:  %v, %vn", len(a), len(b))
}
Golang array concept visualization with boxes representing fixed-size array elements
Go code snippet demonstrating array declaration and initialization syntax
Dmitry Golovach
About

Dmitry Golovach

Principal Network Engineer and AI enthusiast. Always learning, always building.

Share this post

All posts