
2 min read
Slice dimensions
You may recall from last week’s discussion of N-dimensional arrays that Go doesn’t support multi-dimensional arrays. But the implications of this matter a lot more for slices than they do for arrays. Slice types … Like arrays, slices are always one-dimensional but may be composed to construct higher-dimensional objects. With arrays of arrays, the inner arrays are, by construction, always the same length; however with slices of slices (or arrays of slices), the inner lengths may vary dynamically.

2 min read
Slice capacity
Slice types … The array underlying a slice may extend past the end of the slice. The capacity is a measure of that extent: it is the sum of the length of the slice and the length of the array beyond the slice; a slice of length up to that capacity can be created by slicing a new one from the original slice. The capacity of a slice a can be discovered using the built-in function cap(a).

2 min read
Slice storage
Slice types … The length of a slice s can be discovered by the built-in function len; unlike with arrays it may change during execution. The elements can be addressed by integer indices 0 through len(s)-1. The slice index of a given element may be less than the index of the same element in the underlying array. A slice, once initialized, is always associated with an underlying array that holds its elements.

1 min read
Slice types
So we’ve learned about the fixed-length, and therefore somewaht limited, array types. Let’s now look at the more flexible cousin, the slice. Slice types A slice is a descriptor for a contiguous segment of an underlying array and provides access to a numbered sequence of elements from that array. A slice type denotes the set of all slices of arrays of its element type. The number of elements is called the length of the slice and is never negative.

2 min read
Recursive arrays
Today, our final note on array types, and when recursion of types is, and is not, permitted: Array types … An array type T may not have an element of type T, or of a type containing T as a component, directly or indirectly, if those containing types are only array or struct types. // invalid array types type ( T1 [10]T1 // element type of T1 is T1 T2 [10]struct{ f T2 } // T2 contains T2 as component of a struct T3 [10]T4 // T3 contains T3 as component of a struct in T4 T4 struct{ f T3 } // T4 contains T4 as component of array T3 in a struct ) // valid array types type ( T5 [10]*T5 // T5 contains T5 as component of a pointer T6 [10]func() T6 // T6 contains T6 as component of a function type T7 [10]struct{ f []T7 } // T7 contains T7 as component of a slice in a struct ) I think the explanation and provided examples are pretty clear.

1 min read
N-dimensional arrays
There’s one sentence at the end of the paragraph yesterday about arrays, which I saved for today, as I think it deserves special attention: Array types … Array types are always one-dimensional but may be composed to form multi-dimensional types. [3][5]int [2][2][2]float64 // same as [2]([2]([2]float64)) The two examples provided do not represent a 2- and 3-dimensional array, therefore. Rather, they represent an array of arrays, and an array of arrays of arrays, respectively.

2 min read
Array types
Arrays should not be a foreign concept to anyone who’s done programming before. But Go’s version of arrays has some peculiarities that often trip up beginners. Array types An array is a numbered sequence of elements of a single type, called the element type. The number of elements is called the length of the array and is never negative. ArrayType = "[" ArrayLength "]" ElementType . ArrayLength = Expression . ElementType = Type .
Subscribe to Boldly Go: Daily
Every day I'll send you advice to improve your understanding of Go. Don't miss out! I will respect your inbox, and honor my privacy policy.
Unsure? Browse the archive.

2 min read
When a string's length is constant
String Types … The length of a string s can be discovered using the built-in function len. The length is a compile-time constant if the string is a constant. A string’s bytes can be accessed by integer indices 0 through len(s)-1. It is illegal to take the address of such an element; if s[i] is the i‘th byte of a string, &s[i] is invalid. There are a couple of nuggets in this paragraph.
16 min read
What is the Best Book to Learn Go in 2023?
After reading every beginner's book I could find on Go, here is my recommended #1 book to help you learn the language.

3 min read
Immutable strings
String Types A string type represents the set of string values. A string value is a (possibly empty) sequence of bytes. The number of bytes is called the length of the string and is never negative. Strings are immutable: once created, it is impossible to change the contents of a string. The predeclared string type is string; it is a defined type. Anyone who’s ever written even just “Hello, World” in virtually any language has had experience with strings.

2 min read
Numeric type aliases
Numeric Types … byte alias for uint8 rune alias for int32 … To avoid portability issues all numeric types are defined types and thus distinct except byte, which is an alias for uint8, and rune, which is an alias for int32. As with other aliases in Go, this means that the same type simply has two (or perhaps more) identifiers, which are completely interchangeable. This means that byte is not a distinct type, simply backed by a uint8 type.