Go has no classes or inheritance, but it supports the useful parts of object-oriented design: structs hold data, methods attach behaviour to types, interfaces define behaviour implicitly, and struct embedding provides composition. The Go approach is to compose small types and depend on small interfaces rather than build class hierarchies.
This guide maps each classic OOP idea (encapsulation, abstraction, polymorphism, reuse) to the Go feature that covers it, with runnable examples.
A struct groups fields, and a method is a function with a receiver. Together they play the role of a class.
Go has no constructors. A plain function named NewX that returns a ready-to-use value is the convention. Use a pointer receiver (*Account) when the method changes the value or the struct is large, and a value receiver for small immutable types. Keep one style per type.
Visibility is decided by the first letter of a name. Balance is exported and usable from other packages; balance is visible only inside its own package. The unit of encapsulation is the package, not the type, so hide fields and expose behaviour through methods. This protects invariants, such as a balance that must never go negative.
An interface lists method signatures. Any type with those methods satisfies it, with no implements keyword.
Because satisfaction is implicit, the consumer defines the interface. Keep interfaces small (io.Reader has one method) and declare them where they are used, not next to the implementation. A good rule is to accept interfaces and return concrete types.
Embedding a type inside a struct promotes its fields and methods, which gives reuse without inheritance.
This is not subclassing. The Service is not a Logger in the type system, and the embedded type cannot call back into overridden methods of the outer type. If the outer type defines its own Log, it simply shadows the embedded one. Embedding an interface in a struct is also common, for example to wrap and extend a dependency in tests.
Since Go 1.18 you can write type-safe reusable containers and helpers without interfaces and type assertions.
Use generics for algorithms and data structures that work the same across types, and interfaces for behaviour that differs by type.
| OOP concept | Java or C++ | Go |
|---|---|---|
| Class | class | struct plus methods |
| Inheritance | extends | Not available; use embedding |
| Interface | Declared with implements | Satisfied implicitly |
| Constructor | Special method | Function named NewX |
| Access control | public and private | Exported (capitalised) or unexported |
| Method overloading | Supported | Not supported |
Go supports object-oriented design through types, methods, interfaces and composition, but it has no classes or inheritance. The Go FAQ describes it as both and neither, depending on your definition.
No. Embedding gives you the promoted methods and fields of another type, but it is composition and does not create a subtype relationship.
Use a pointer receiver when the method mutates the receiver, when the struct is large, or when it contains a mutex. Do not mix both kinds on one type.
In the package that consumes them. This keeps interfaces small and lets any type satisfy them without importing your package.
Go achieves object-oriented design with structs and methods, package-level encapsulation, implicit interfaces for polymorphism and embedding for composition. Design with small interfaces, prefer composition over hierarchy, and use generics where behaviour is identical across types.