
1 min read
Pointer types
After the long discussion of the details and nuances of struct types, we have a simple topic: Pointers! Well, maybe it’s not that simple. There are a lot of subtleties that go into the proper use of pointers. But from a specification standpoint, it’s simple. This is the entirety of the section: Pointer types A pointer type denotes the set of all pointers to variables of a given type, called the base type of the pointer.
33 min watch
How do you test filepath.Abs in Go?
I provide three ways to tackle the question: How do you test filepath.Abs failure in your Go code?

2 min read
Recursive struct types
Finally, after more than a week, we’re at the end of our discussion of Go’s struct types. You may recall that recursion is not permitted within array types. We have a similar, though not identical, restriction for structs: Struct types … A struct type T may not contain a field 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.

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.
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
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.
33 min watch
Go Code Roast: Logstash to Prometheus Exporter
I'm back with another Go Code roast! This one comes from Reddit, and is for the logstash-exporter package.

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.

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.

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.