Hi everyone! this is Jimmy , and this is the second article in my series “Breaking Things with Go.” In this series, I document my journey through Jon Bodner’s Second Edition: Learning Go – An Idiomatic Approach to Real-World Go Programming and explore how to use Go in the most practical way I can

in this series the resources are the book itself, go documentation, and any AI model to clarify some things lets jump into it Jump Into A Hole Stickers - Find & Share on GIPHY


in this Break We will talk about Composite Data Types

What are composite types ? A data type built from other types, allowing for heterogeneous data (different types) in one structure

and there is some of them in Go but here we will talk about Arrays and their shortage only

Arrays

  • Array is a data structure that stores a collection of items (like numbers, words, or objects) of the same type in a single, ordered list, allowing you to manage many values under one variable name
  • all elements of an array must be of the same specified type
  • if not given literal the initialized array will store the zero value of the type
  • two arrays are said to be equal if they have the length and contain equal values
  • arrays are 0 indexed so first element has index 0
  • we can get array length using len(x)
  • Assignment in Go copies the entire array to become a different array (no sharing)

multiple declaration ways

go
var x [3]int // [0, 0, 0] zero value of the type
var y [3]string // [   ] zero value of the type ""
fmt.Println(y[0] == "") // true
var z = [3]int{1, 2, 3} // [1, 2, 3]
var a = [...]int{1, 2, 3} // [1, 2, 3]
var b = [3]int{1} // [1, 0, 0]

we can also declare sparse array(most of its values set to zero so we specify the nonzero indices only) as next

go
var x = [12]int{1, 5:4, 6, 10:100, 15} // [1 0 0 0 0 4 6 0 0 0 100 15]
                                       //  ^         ^ ^        ^   ^
                                       //  0         5 6        10  11

Go doesn’t have true syntactic multidimensional array literals like some languages but we can stimulate var x [2][3]int so this create array of length 2 whose type is an array of int of length 3

we can read specific elements using [index] and remember we said 0 indexed

go
a := [...]int{1, 2, 3} 
fmt.Println(a[0]) //prints 1
fmt.Println(a[1]) //prints 2
fmt.Println(a[2]) //prints 3
fmt.Println(len(a)) // 3

and care the out-of-bounds access

  1. you can’t read past the end of an array
  2. you can’t write past the end (as we know arrays are fixed size)
  3. you can’t use negative index to read from the end

and doing any of those using literal or constant variable causes compile-time error but doing it with declared variable causes runtime panic

go
a := [...]int{1,2,3}
i := 3
const z = 3
fmt.Println(a[i]) //runtime panic
fmt.Println(a[3]) //compile time error 
fmt.Println(a[z]) //compile time error

Limitations and problems

  • Compiler demands arrays’ size to be known at compile time

    • we have to declare the size of an array using literal or constant of the compiler will figure it out at compile-time if we used […]
  • Go consider size of the array are part of the type itself so int array of length 3 has type [3]int but of length 2 has type of [2]int and those are very different types

so where is the problem ? for example:

  • you can’t convert between two arrays of different size
  • functions can’t accept array of any size so if the parameters is unknown size we can’t pass it to the function

Bottom of the Line → Go Arrays are :

  1. Fixed Size
  2. Value types
  3. meant for low-level, predictable memory layout

so don’t use them unless you know the exact length you need ahead of time

and that’s why Arrays aren’t used that much in Go

Takes

  • unlike some other languages, arrays in Go are value types
  • no sharing assignment creates a new copy
  • array size is part of the type in go

Coming Next

the next break will be about Slices and It’s gonna be a long break so stick around for next break where we will break more stuff

feel free to reach out Peace Out GIFs | Tenor