Function types

Go has first-class functions. That is to say, functions are treated as any other data type, and can be passed around as arguments, used as struct field types, etc. Function types A function type denotes the set of all functions with the same parameter and result types. The value of an uninitialized variable of function type is nil. FunctionType = "func" Signature . Signature = Parameters [ Result ] . Result = Parameters | Type .

How-Tos

10 min watch


Use error decorators to simplify your error handling code

Learn to use a custom error type as a decorator to simplify error handling.


Pointers to interfaces

As I mentioned last week, a pointer in Go may have any base type. This includes interfaces. type MyInterface interface { DoStuff() } var x *MyInterface var y *interface{} var z ****interface{ Foo() } Now while these are all valid, they are rarely useful. And this often trips people up. As a rule: Never use a pointer to an interface. There is only one exception of which I am aware. And it’s so unusual, that you can generally just adhere to the above rule.


nil pointers

Yesterday we learned about pointers. But beyond what the spec says, there are a few notes worth calling out. To start with, as mentioned in the spec, the zero value for a pointer is nil. But it’s still a typed nil value, as we can see by using the %T printf argument, which displays the variables type. Expanding on yesterday’s example: var x int var y *int var z **int // Prints: 0 <nil> <nil> <nil> fmt.


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.

Code Review

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?


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.

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.


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.


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.

Code Review

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.


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.