Method Syntax in Go
Understand the syntax for declaring methods in Go, from the receiver parameter to binding a method to its base type.
A method in Go is a function that carries an extra parameter, the receiver, placed between the func keyword and the method name. That single parameter is what turns an ordinary function into a method and what ties the method to a specific type. If you have written functions in Go, methods will feel like a small, natural extension.
Method Declaration Syntax
A method declaration follows this structure:
func (receiver) MethodName(parameters) ReturnType {
// body
}
A concrete example with a Book type:
type Book struct {
pages int
}
func (b Book) Pages() int {
return b.pages
}
The receiver is (b Book). Everything after it, Pages() int, is a standard function signature. The receiver acts like a regular parameter, but it appears before the method name and dictates which type the method belongs to.
The Go specification formalises this as:
MethodDecl = "func" Receiver MethodName [ TypeParameters ] Signature [ FunctionBody ] .
Receiver = Parameters .
The [ TypeParameters ] part is new in Go 1.27: since that release a method may declare its own type parameters, in addition to any the receiver type already has. Methods on generic types are covered in their own chapter.
A receiver is syntactically identical to a parameter list, but it must contain exactly one parameter, no more, no less. That single parameter's type is called the receiver type. The underlying type of the receiver (stripping any pointer indirection) is the receiver base type, and the method is said to be bound to that base type.
Methods vs. Functions:
A method is just a function with a receiver. If you take the receiver away, you get a plain function. This means methods are not a separate language construct; they are declared with the same func keyword and follow the same scoping and visibility rules.
The Receiver Parameter
The receiver is the single most distinctive part of method syntax. Consider this method on a Player type:
type Player struct {
name string
score int
}
func (p Player) DisplayName() string {
return p.name
}
The identifier p is the receiver name. Inside the method body, p is a local variable holding a copy of the value the method was called on. You can name it anything an identifier allows, but by convention Go uses a short name, often the first letter of the receiver type in lowercase. That convention eliminates repetitive this. or self. prefixes and keeps the code clean.
A receiver type can be the type itself or a pointer to it:
// Value receiver
func (p Player) DisplayName() string { ... }
// Pointer receiver
func (p *Player) SetScore(s int) { ... }
Both forms are syntactically identical apart from the *. The syntax, however, is always the same: func followed by the receiver in parentheses, then the method name.
Receiver name omission:
You do not have to give the receiver a name. If the method body never uses the receiver value, you may leave the name out entirely, as in func (Book) Kind() string, or write _ to signal explicitly that it is unused. A name is only required when the body refers to the receiver.
Binding a Method to Its Base Type
When you write a method declaration, you are doing two things at once: declaring the method itself and associating it with the receiver's base type. The compiler treats the method as part of that type's identity.
Consider the Book type again:
type Book struct {
pages int
}
func (b Book) Pages() int {
return b.pages
}
Pages is now bound to Book. In practice this means you call it on a value of type Book:
b := Book{pages: 320}
fmt.Println(b.Pages()) // 320
The compiler also lets you refer to the method as a plain function value, called a method expression. Book.Pages on its own denotes a function of type func(Book) int, with the receiver as its first argument, and you can call it directly:
fmt.Println(Book.Pages(b)) // 320
This is the same call as b.Pages(), written out in full. For day-to-day code you will use the value.Method() form; the method expression form is useful when you want to pass the method itself around as a function value.
Checkpoint:
If you declare a method and can call it with value.MethodName(), and you also see the compiler recognises Type.MethodName as a valid function reference, the binding is working correctly.
Allowed Receiver Types
Not every type can have methods. The Go spec places four requirements on the receiver base type T:
Tmust be a defined type. That means you must create it with atypedeclaration (type MyInt int,type Book struct{...}, etc.). An unnamed composite type such as[]bytecannot have methods, because it is not a defined type: the receiver base type must be a name you declared.Tmust be defined in the same package as the method. You cannot add methods to a type imported from another package, not even to standard library types. This is a deliberate design choice that keeps types predictable. It is also why predeclared types likeintorstringcannot have methods: they are not declared in your package, so the compiler rejects the method withcannot define new methods on non-local type int.Tmust not be a pointer type. You cannot writefunc (p *MyInt) ...whereMyIntis itself defined as a pointer type. The receiver type can be a pointer to a non-pointer base type, but the base type itself cannot be a pointer.Tmust not be an interface type. Methods are declared on concrete types. Interface types describe method sets that concrete types must satisfy; they themselves are not concrete and cannot have method implementations.
Examples that illustrate the rules:
// Allowed: defined non-pointer type in same package
type Counter int
func (c Counter) Value() int { return int(c) }
// Allowed: struct type. A method and a field of the same struct
// must have different names, so the field is FullName here.
type Employee struct { FullName string }
func (e Employee) Name() string { return e.FullName }
// Allowed: pointer receiver on non-pointer base type
func (e *Employee) SetName(name string) { e.FullName = name }
// Not allowed: base type is a pointer
type Ptr *int
// func (p Ptr) Something() {} // compile error
// Not allowed: type defined in another package
// func (s string) Len() int {} // compile error
// Not allowed: interface type
// type Reader interface { Read([]byte) (int, error) }
// func (r Reader) DoSomething() {} // compile error
Receiver not in local package:
Attempting to declare a method on a built-in type like func (s string) Len() int will produce a compile error: "cannot define new methods on non-local type string." You can work around this by defining a new named type based on the built-in type within your package, and then declaring methods on that.
Implicit Method for Pointer Types
When you declare a method with a value receiver on a type T, that method is also part of the method set of *T. In other words, a pointer to a Book can call every value-receiver method of Book, because Go dereferences the pointer automatically.
Given this declaration:
func (b Book) Pages() int {
return b.pages
}
You can call Pages on a *Book directly:
b := &Book{pages: 200}
fmt.Println(b.Pages()) // works, the pointer is dereferenced for the call
This rule is part of method syntax because it explains why a method declared with a value receiver is usable through a pointer without any extra code. The reverse also works, but only for addressable values: calling a pointer-receiver method on a value is shorthand for taking the value's address first, so c.Increment() on a local variable means (&c).Increment(). It does not work on values with no stable address, such as a function return value or a map element.
Common Mistakes When Declaring Methods
- Forgetting the receiver parentheses. The receiver must be enclosed in parentheses, even if it is a single identifier with no pointer notation:
func (r Rectangle) Area(), omitting the parentheses is a syntax error. - Using a type from another package as the receiver base type. If you need methods on a type you didn't define, you can embed it in a local struct or create a new defined type based on it.
- Declaring a method on an interface. Methods belong to concrete types. If you want to provide an implementation of an interface, you declare methods on a concrete type that satisfies the interface.
- Assuming the receiver name must be
thisorself. Go style prefers a short abbreviation of the type name. Usingthisis not idiomatic, thoughgo vetdoes not flag it; it is a style convention, not a compiler rule.
Unused receiver:
If you declare a method with a receiver you never use, you may leave the receiver name out or write _. However, a method that ignores its receiver is often a sign that it should be a plain function instead.
Summary
Method syntax in Go is essentially a function signature with a receiver parameter placed in front of the method name. That receiver binds the method to its base type, and the binding works only when the base type is a defined type in the current package, is not a pointer type, and is not an interface. A value-receiver method is also available on pointers to the type, because the method set of *T includes all value-receiver methods of T.
A method declaration is a single statement that tells the compiler three things: the name of the method, its signature, and which type it belongs to. Understanding that structure is the foundation for value vs pointer receivers, method sets, and interfaces, all of which build directly on this syntax.