Go Language Spec —2 min read
Empty structs

We finally we have enough knowledge for the EBNF format not to seem completely foreign, so let’s jump back and take a look at that, with the examples provided in the spec… Struct types … StructType = "struct" "{" { FieldDecl ";" } "}" . FieldDecl = (IdentifierList Type | EmbeddedField) [ Tag ] . EmbeddedField = [ "*" ] TypeName [ TypeArgs ] . Tag = string_lit . // An empty struct.

Go Language Spec —2 min read
Struct tags

Struct types … A field declaration may be followed by an optional string literal tag, which becomes an attribute for all the fields in the corresponding field declaration. An empty tag string is equivalent to an absent tag. The tags are made visible through a reflection interface and take part in type identity for structs but are otherwise ignored. struct { x, y float64 "" // an empty tag string is like an absent tag name string "any string is permitted as a tag" _ [4]byte "ceci n'est pas un champ de structure" } // A struct corresponding to a TimeStamp protocol buffer.

Go Language Spec —2 min read
Struct method promotion

Yesterday we saw an example of struct field promotion. But methods (which we haven’t really discussed yet) can also be promoted. Struct types … Given a struct type S and a named type T, promoted methods are included in the method set of the struct as follows: If S contains an embedded field T, the method sets of S and *S both include promoted methods with receiver T. The method set of *S also includes promoted methods with receiver *T.

Go Language Spec —2 min read
Struct field promotion

Yesterday we learned that structs can have embedded fields. Although we didn’t really learn about any of the special powers this gives us. Today we’ll have a look at those powers. One advantage to using an embedded type is that the implicit field name (the one derrived from the type, Person, in our example) can be omitted. This is the result of “promotion”. For example: var e Employee e.Name = "Bob" // equivalent to e.

Go Language Spec —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?

Go Language Spec —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)

Go Language Spec —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.

Go Language Spec —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.

Go Language Spec —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).

Go Language Spec —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.

Go Language Spec —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.