Methods aren't for "objects"

July 24, 2023

I’ll be livestreaming again today, in just a few minutes. Join me live, or watch the replay. I’ll be picking up where I left off, with my new linter project.


One of the most powerful things I like about Go, is the ability to put methods on any type.

Type definitions

…

Type definitions may be used to define different boolean, numeric, or string types and associate methods with them:

type TimeZone int

const (
	EST TimeZone = -(5 + iota)
	CST
	MST
	PST
)

func (tz TimeZone) String() string {
	return fmt.Sprintf("GMT%+dh", tz)
}

In many other languages, if you want to associated a method with, say, an integer, you have to wrap that integer in an Object. Not so in Go. With Go, you can just define a custom type, with an underlying type of int, and put methods on it!

You can even put methods on function types, which seems a bit strange at first, but is also very powerful. There’s actually a good chance you’ve already used this pattern, perhaps without realizing it, if you use the net/http package. The type HandlerFunc is such an example:

type HandlerFunc func(ResponseWriter, *Request)

This type, which is a function type, has a single method defined on it:

func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request)

Quotes from The Go Programming Language Specification Version of December 15, 2022


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Method values

A quick reminder: In today’s live stream, I’ll be deploying a Go app to Kubernetes! I hope you’ll join me! Today we continue the explanation of Method values. There’s nothing too surprising today. The spec mostly just confirms what should seem natural… Method values … As in the discussion of method expressions above, consider a struct type T with two methods, Mv, whose receiver is of type T, and Mp, whose receiver is of type *T.


Method values

As I promised yesterday, today we’re looking at method values, a concept with some similarties to the previous concept of method expressions, but with a bit more utility. Method values If the expression x has static type T and M is in the method set of type T, x.M is called a method value. The method value x.M is a function value that is callable with the same arguments as a method call of x.


Method expressions, part 2

Today I’ll continue where I left off yesterday, talking about… Method expressions … Similarly, the expression (*T).Mp yields a function value representing Mp with signature func(tp *T, f float32) float32 The key difference to notice between this example and yesterday’s is that the first argument (that of the receiver) is a pointer here, but was not a pointer in the previous example: func(tv T, a int) int // First argument of type T, not a pointer func(tp *T, f float32) float32 // First argument of type *T, a pointer For a method with a value receiver, one can derive a function with an explicit pointer receiver, so

Get daily content like this in your inbox!

Subscribe