2 min read
Embedded struct fields
When I introduced structs last week, I skipped over one sentence. Today I’m going to address that. Struct types A struct is a sequence of named elements, called fields, each of which has a name and a type. Field names may be specified explicitly (IdentifierList) or implicitly (EmbeddedField). Within a struct, non-blank field names must be unique. We already saw how field names are expressed explicitly. But what is an embedded field?
3 min read
Blank field names
Yesterday we started talking about Go’s structs, and breezed over a phrase about non-blank field names. Wassat? Struct types … Within a struct, non-blank field names must be unique. Go has a concept of a blank identifier. It looks like the underscore character (_), and is useful in many situations, and we’ll discuss more of them in due time. (It’s also decidedly not useful in some situations where it’s permitted, and I made a video about that a while ago)
2 min read
Struct types
Structs. Now we’re getting to some meaty stuff! Let’s start simple. What is a struct? Struct types A struct is a sequence of named elements, called fields, each of which has a name and a type. … Within a struct, non-blank field names must be unique. Forget about blank field names for now. I’ll talk about that tomorrow. I’m also postponing the EBNF description of structs for a while, because there are actually several subtleties to how structs work in Go, which can get into the weeds very quickly.
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.
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
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 .
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.