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

Related Content

Type parameters in method definitions

Yesterday we saw that when a method is defined on a generic type, the receiver must include type parameters. Now for all the relevant details: Method declarations … … Syntactically, this type parameter declaration looks like an instantiation of the receiver base type: the type arguments must be identifiers denoting the type parameters being declared, one for each type parameter of the receiver base type. The type parameter names do not need to match their corresponding parameter names in the receiver base type definition, and all non-blank parameter names must be unique in the receiver parameter section and the method signature.

Methods on generic types

Ready to dive back into generics, this time with regard to methods? Great, ‘cause here we go! Method declarations … If the receiver base type is a generic type, the receiver specification must declare corresponding type parameters for the method to use. Okay. Before we continue, let’s make sure we all know exactly what we’re talking about. What does it mean for a receiver base type to be a generic type?

Receiver uniqueness

Today I’ll be love coding again. I hope you can join me! I’ll be continuing where I left off, working on the backlog for my open-source CouchDB SDK, Join me to see how many mistakes a senior Go dev makes while coding. Method declarations … A non-blank receiver identifier must be unique in the method signature. If the receiver’s value is not referenced inside the body of the method, its identifier may be omitted in the declaration.