Method declarations

August 24, 2023

Now that we’ve covered function declarations, let’s discuss the more nuanced topic of method declarations, which can be thought of as special case of of function declarations.

Method declarations

A method is a function with a receiver. A method declaration binds an identifier, the method name, to a method, and associates the method with the receiver’s base type.

MethodDecl = "func" Receiver MethodName Signature [ FunctionBody ] .
Receiver   = Parameters .

Before we dive into the details and nuances over the next few days, let’s summarize with a simple example:

type Person struct {
	Name string
}

func (p *Person) Hello() {
	fmt.Printf("Hello, %s\n", p.Name)
}

Here Hello is declared as a method of *Person. This is evident by the fact that it has a receiver (the function argument that preceeds the name of the function).

As an aside, there are two ways to call a method in Go. The most natural/obvoius one, you likely can guess, or have seen:

var p Person{Name: "Bob"}

p.Hello() // Call the Hello() method on the p variable

The other, which you’ll probably never need, but also exists, is to pass the receiver as a direct argument to the fully qualified method name. This doesn’t require defining a variable first:

(*Person).Hello(&Person{Name: "Alice"})

See it in the playground

Quotes from The Go Programming Language Specification Version of August 2, 2023


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 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


Method expressions

There was no livestream today, as I was a bit under the weather. I intend to be back live streaming next Monday as usual Method expressions If M is in the method set of type T, T.M is a function that is callable as a regular function with the same arguments as M prefixed by an additional argument that is the receiver of the method. MethodExpr = ReceiverType "." MethodName .

Get daily content like this in your inbox!

Subscribe