Plurrrr

Sun 11 Sep 2022

Neither self nor this: Receivers in Go

Go does not have classes and objects, but it does have types that we can make many instances of. Further, we can attach methods to these types and they kind-of start looking like the classes we’re used to. When we attach a method to a type, the receiver is the instance of the type for which it was called.

Choosing the name of a receiver is not always a trivial task. Should we be lazy and name them all the same (like this or self)? Or treat them not unlike local variables by abbreviating the type (like srv to a Server type )? Or maybe something even more nuanced?

And what are the consequences? How will our code suffer if we choose one approach over the other? Let's explore.

Source: Neither self nor this: Receivers in Go, an article by Andrey Petrov.

The case for self receivers in Go

Ok ok, hear me out. I know you're already lighting torches and sharpening your pitchforks (Side note: Can you sharpen a pitchfork?). I know I'm probably in the minority here, so please allow the little-guy to make a small case for the use of self as a receiver name.

Source: The case for self receivers in Go, an article by Cody Potter.