Go Structs and Interfaces — Grouping Data and the Method Set That Defines Behavior
A struct groups fields into one value, a method attaches behavior to a type, and an interface names a set of methods that a type satisfies just by having them. This guide runs four programs. The main program builds a Point, prints it as (3,4) through its String method, and passes a Rect to a function that accepts any Shaper, printing area=30 and iface area=6. The other programs show a pointer receiver changing a value that a value receiver cannot, a struct copy that leaves the original alone, and an interface holding a typed nil pointer that is not itself nil.
Written November 11, 2026 · Tested with Go 1.27 · 13 min read
- The main program: a struct, a method, and an interface
- Structs: fields grouped into one value
- Methods: functions with a receiver
- Interfaces: satisfied by having the methods
What this guide is
Go has no classes. A program models data with structs, which are values that group named fields, and it models behavior with methods, which are functions attached to a type. Interfaces sit on top of both: an interface type names a set of methods, and any type that has those methods satisfies the interface. Nobody declares that a type satisfies an interface. The compiler checks the method set, and that is the whole contract. This guide runs one main program that uses a struct, a method, and an interface together, plus three smaller programs that isolate the receiver rule, the struct copy rule, and the nil interface trap.
The main program: a struct, a method, and an interface
package main
import "fmt"
type Point struct{ X, Y int }
func (p Point) String() string { return fmt.Sprintf("(%d,%d)", p.X, p.Y) }
type Shaper interface{ Area() int }
type Rect struct{ W, H int }
func (r Rect) Area() int { return r.W * r.H }
func describe(s Shaper) { fmt.Printf("area=%d\n", s.Area()) }
func main() {
p := Point{3, 4}
fmt.Println("point", p)
r := Rect{5, 6}
describe(r)
var s Shaper = Rect{2, 3}
fmt.Printf("iface area=%d\n", s.Area())
}
The exact output:
point (3,4)
area=30
iface area=6
Three different mechanisms produced those three lines. The struct literal Point{3, 4} built the value, the String method decided how it printed, and the Rect values reached Area once through a function parameter typed as an interface and once through a variable typed as an interface. The sections below take each mechanism in turn.
Structs: fields grouped into one value
The declaration type Point struct{ X, Y int } defines a type with two fields, X and Y, both of type int. The literal Point{3, 4} fills the fields in declaration order, so X is 3 and Y is 4. A field is read with a dot: p.X is 3 and p.Y is 4. A literal can also name its fields, as in Point{X: 3, Y: 4}, which builds the same value and stays readable when a struct gains more fields. A field left out of a literal takes the zero value for its type, which for int is 0.
A struct is a value, not a reference to an object somewhere else. Assigning a struct copies it, and passing a struct to a function copies it. That one fact explains both the receiver rule and the copy trap later in this guide. A struct can also hold another struct as a field, and it can hold slices and maps, which are reference-like values whose shared behavior the slices and maps guide on this site covers. Fields and methods are separate in Go: the fields live in the struct declaration, and the methods are declared outside it, in the same package.
Methods: functions with a receiver
A method is a function declared with a receiver, the value in parentheses before the method name. The main program declares func (p Point) String() string, which attaches a String method to the Point type. Inside the method, p is the value the method was called on, and p.X and p.Y read its fields.
The String method does visible work in the main program. The fmt package looks for a String() string method when it prints a value, and calls it when the type provides one. That is why fmt.Println("point", p) printed point (3,4) instead of the default field list. The method turned a pair of integers into a formatted string, and the print call used the method without naming it. A type with no String method prints in the default form, braces and field values, which is also useful when debugging, and one reason to add String only when a type has a natural short form.
Methods are how a type gains behavior without a class hierarchy. The receiver can be any type defined in the same package, not only a struct: a named integer or string type can carry methods too. What a method cannot do is attach to a type from another package, so behavior for outside types is written as plain functions that take the value as an argument.
Interfaces: satisfied by having the methods
The declaration type Shaper interface{ Area() int } defines an interface with one method. Any type that has an Area() int method satisfies Shaper. Rect has that method, declared as func (r Rect) Area() int, so Rect satisfies Shaper. There is no declaration of that fact anywhere in the program: no implements clause, no registration, no import of the interface by the type. The function describe takes a Shaper, and the call describe(r) compiles because the compiler checks the argument's method set against the interface and finds Area.
This arrangement reverses the usual direction of a contract. The code that needs the behavior defines the interface, in this case a one-method interface next to the function that uses it, and the types that supply the behavior need to know nothing about it. A type written before the interface existed satisfies it the moment the interface is defined, as long as the methods match in name and signature. A method with the same name but a different signature does not match: an Area() float64 method would not satisfy Shaper, because the return type is part of the signature.
Small interfaces are the habit this leads to. An interface with one or two methods is easy for a new type to satisfy and easy to substitute in a test. An interface with many methods is a large contract, and few types will satisfy it by accident. The standard library leans on small interfaces for exactly this reason, and the String method in the main program satisfies one: the fmt package defines a Stringer interface with the single method String() string, and Point satisfies it by having that method.
Interface values: a concrete value behind the interface
The last two lines of the main program come from a variable typed as the interface itself: var s Shaper = Rect{2, 3}. The variable s holds a concrete Rect value behind the Shaper type. Calling s.Area() runs the Rect method on the stored value, a 2 by 3 rectangle, and prints iface area=6. The static type of s is Shaper, so code holding s can call only the methods the interface names, even though the value behind it is a full Rect with fields W and H.
An interface value is really two things: a concrete type and a value of that type. That pair explains the nil trap in the last section of this guide. It also explains how a variable like s can hold different concrete types at different times: any value whose type satisfies Shaper can be assigned to it, and each call to Area runs the method of the type currently stored. Code that needs the concrete value back uses a type assertion, which the Go Tour interfaces pages describe; the working rule here is that most code should call the interface methods and leave the concrete type alone.
Pointer receivers change the value; value receivers get a copy
The main program's methods all use value receivers: func (p Point) and func (r Rect). A value receiver works on a copy of the value, which is right for methods that only read, like String and Area. A method that must change the value needs a pointer receiver, and a second program shows the difference with two scaling methods on the same type:
package main
import "fmt"
type Rect struct{ W, H int }
func (r Rect) ScaleVal(f int) { r.W *= f; r.H *= f }
func (r *Rect) Scale(f int) { r.W *= f; r.H *= f }
func main() {
r := Rect{2, 3}
r.ScaleVal(10)
fmt.Println("after value scale", r.W, r.H)
r.Scale(10)
fmt.Println("after pointer scale", r.W, r.H)
}
The exact output:
after value scale 2 3
after pointer scale 20 30
ScaleVal multiplied its copy by 10 and the copy was discarded when the method returned, so the original still reads 2 and 3. Scale has a pointer receiver, so it worked on the original value, which now reads 20 and 30. Go passes the address automatically for a method call on an addressable value, so the call reads r.Scale(10) rather than (&r).Scale(10).
The receiver choice also affects the method set, and so it affects interfaces. A type's value method set contains only the methods declared with value receivers, while the pointer's method set contains both kinds. If Area had been declared on *Rect, a plain Rect value would not satisfy Shaper, though a *Rect would. The rule of thumb follows from both facts: use a pointer receiver when the method changes the value, use a value receiver when it only reads, and keep the choice consistent across a type's methods, because mixing them makes the method sets harder to predict.
Trap: copying a struct copies its fields
package main
import "fmt"
type Point struct{ X, Y int }
func main() {
p := Point{3, 4}
q := p
q.X = 99
fmt.Println("p", p.X, p.Y, "q", q.X, q.Y)
}
The exact output:
p 3 4 q 99 4
Changing q.X left p.X at 3. The two variables hold separate copies of the field values, and each can change without touching the other. The trap is expecting either of the other two behaviors: a shared reference, where the change would show in both, or a compiler warning that a copy was made. Neither happens. One qualification belongs with the rule: a field that holds a slice or a map copies the descriptor, not the contents behind it, so two struct copies can still share a slice's elements. Plain value fields like the integers here copy in full.
Trap: an interface holding a nil pointer is not nil
package main
import "fmt"
type Shaper interface{ Area() int }
type Rect struct{ W, H int }
func (r Rect) Area() int { return r.W * r.H }
func main() {
var s Shaper
fmt.Println("nil interface:", s == nil)
var r *Rect
var s2 Shaper = r
fmt.Printf("typed nil: isNil=%v type=%T\n", s2 == nil, s2)
}
The exact output:
nil interface: true
typed nil: isNil=false type=*main.Rect
The first variable was never assigned, so it is nil. The second holds a *Rect that happens to be nil, and the interface reports isNil=false with a concrete type of *main.Rect. The practical failure looks like this: a function returns a typed nil pointer wrapped in an interface, the caller tests the result against nil, the test passes it as a real value, and a later method call fails on the empty value behind the interface. The fix is at the return site: return a plain nil interface when there is no value, not a typed pointer that happens to be nil. Testing an interface against nil checks the pair, never the value inside it.
Which form for which job
| Job | Form | Why |
|---|---|---|
| Group related fields into one value | struct | Fields travel together, copy as a unit, and read with a dot |
| Print a type in a natural short form | a String() string method |
The fmt package calls it, and the type satisfies the Stringer interface |
| A function that works on anything with one behavior | a small interface parameter, like describe taking Shaper |
Any type with the method qualifies, with no declaration linking them |
| Change a value inside a method | pointer receiver | A value receiver changes only its copy, as the 2, 3 result shows |
| Read or compute inside a method | value receiver | The copy is enough, and both the value and its pointer then satisfy interfaces |
| Report that no value exists | return a plain nil interface | A typed nil pointer in an interface compares as non-nil |
The deciding question is what the code needs to promise. A struct promises a shape, an interface promises a method, and the receiver kind promises whether a method can change the value it was called on.
Checklist
- Before modeling data, reach for a struct: fields grouped in one value beat a row of loose variables that must travel together by convention.
- Before adding a method, choose the receiver on purpose: pointer when the method changes the value, value when it only reads, and stay consistent across the type.
- Before writing an interface, keep it small: one or two methods are easy to satisfy, and a type satisfies the interface by having the methods, with no declaration.
- Before passing a value to an interface parameter, check the method set: a pointer-receiver method is not in the value's method set.
- Before relying on a struct assignment, remember it copies: value fields are independent after the copy, slice and map fields still share their contents.
- Before returning an interface from a function, return a plain nil for no value: a typed nil pointer compares as non-nil and reaches the caller as a false value.
Related guides on this site
- Go Slices and Maps — Append, Length, Capacity, and Lookup — the containers that struct fields so often hold, including why a copied slice descriptor still shares its elements.
- Finding a Leaked Goroutine with Go 1.27's goroutineleak Profile — a debugging guide in the same Go series, reading a leak profile to find a goroutine that never exits.
- Go errgroup Worker Pool: The Race the Compiler Won't Catch — a worker pool built on the same struct and method mechanics, where the failure is a race.
- C++ Classes and Objects: Your First Class, Constructor, and Methods — the C++ counterpart, where behavior is declared inside the class instead of attached by receivers, and interfaces are written as abstract classes.
Sources
- Go documentation, Effective Go — Structs — struct literals, field promotion in the surrounding sections, and how composite literals build values.
- Go Tour, Methods — methods, receivers, and the pointer receiver rule for methods that modify the value.
- Go Tour, Interfaces — interfaces satisfied implicitly, interface values, and the nil interface value.
- Go documentation, Effective Go — Interfaces and other types — interfaces as a set of methods, and how a type satisfies an interface by having the methods.
Keep Exploring
The full Go hub \u2014 slices and maps, concurrency, and the standard library.
Every guide on the site in one list, grouped by topic.
Every browser-only checker and verifier on the site.
The C++, Python, Sysadmin, Rust, Go, and TypeScript hubs.