Error Sorter — Go

Sort Go errors at Mbak Septi's property tax post: errors.Is, errors.As, interfaces and maps of structs. 14 levels, right in the browser.

What you learn in this game

In Go, a failure is not thrown. A function that can fail returns two values: its result, then an error last, and nil means no error. At Ms. Septi's PBB post, the cekpbb service and every program that calls it are written in Go 1.27. Every program you complete is built and run by real Go, and the board on the post copies exactly what it prints.

An error rarely reaches its caller directly. Every function along the way adds a note with fmt.Errorf, like an envelope put into another envelope. The printed text can be exactly the same, but what the caller needs is the original error, and it only comes along when every layer keeps it.

An interface variable holds two things: the type of the value inside it and the value. It is equal to nil only when both are empty. A nil pointer put into an interface still carries its type, so that interface is no longer nil.

An interface does not care about the type underneath, as long as the methods are there. But it still holds a value, and a struct value put into it is copied. A pointer put into an interface is copied too, only a copy of a pointer points at the same struct.

A Go map holds values. When the value is a struct, every time you read m[k] you get a copy of it. That is why changing a struct inside a map needs care: change the copy and store it again, or store pointers to structs in the map.