Structs & Methods
A struct groups named fields into one type, and a method is a func bound to a type through a receiver. Together they are how Go models data and behaviour. But Go is unlike class-based languages here: a struct is a value, not an object with reference identity; a method is an ordinary function whose first argument is written in a special syntax; and there is no "inheritance" in the usual sense at all.
This topic walks through it layer by layer. First the struct itself as a value, then the method and its receiver, then the choice between value and pointer receivers (who sees a mutation), then embedding — the syntax mistaken for inheritance, though it is composition — and finally the constructor idiom Go uses in place of the constructors it lacks. Every example keeps the differences from C++/Java in view: those are precisely what gets asked.
Topic map
- Structs — a
structas a composite type of named fields and, above all, as a value: assignment and passing into a function copy every field; the zero value is ready to use; field tags are just a string forreflect. - Methods — a method is a
funcwith a receiver (func (c Counter) Inc()); it is declared on a named type in the same package (usually astruct); you call it asc.Inc(). - Pointer Receiver — a value receiver operates on a copy (the mutation is lost), a pointer receiver
*Ton the original; the receiver type determines the method set and interface satisfaction. - Embedding — an unnamed field
type B struct{ A }embedsA;A's fields and methods are promoted intoB; this is composition, not inheritance — there is no polymorphism, and an own field/method shadows the embedded one. - Constructor Idiom — Go has no constructors; the idiom is a
NewT(...) *Tfunction that validates and returns a ready value; often the zero value is usable without one.
Common mistakes and traps
| Mistake | Consequence |
|---|---|
Treating a struct as an object and expecting b := a to share state | It is a full copy of every field; for a mutation to be visible you need a pointer |
| Thinking a method is something separate from a function | A method is a func with a receiver; c.Inc() is sugar over Inc(c) |
| Declaring a method on a type from another package | Not allowed — a method may be declared only on a type in its own package |
| Changing a field on a value receiver and expecting an effect outside | The copy is mutated; to mutate the original you need a pointer receiver *T |
| Calling embedding inheritance | It is composition with promotion; no subtyping or polymorphism, an own field merely shadows the embedded one |
Looking for a constructor or new-like syntax in Go | There are no constructors; the idiom is a NewT(...) function; often the zero value is enough |
Why it matters for interviews
Structs and methods are the junior-level filter for "understands Go's value model" versus "thinks in C++/Java classes." The syntax itself is trivial, but the differences are asked almost every time.
What interviewers usually check:
- Why a
structis a value and what exactly is copied on assignment and on passing into a function. - What a method is and how it differs from a function, and on which types it can be declared.
- When a pointer receiver is needed and how the receiver type affects interface satisfaction.
- How embedding differs from inheritance and why Go has no polymorphism through it.
- How Go gets by without constructors, and when to write
NewTversus when the zero value is enough.