Object-oriented programming organizes concepts around data and operations. In Go, use structs for object-like data, methods for manipulation, and interfaces for required behavior rather than explicit inheritance. section 3.2
Listing 3.6 The same idea in Go
package main
import (
"fmt"
)
type Animal interface { #1
speak()
}
type Cat struct {} #2
func (c Cat) speak() {
fmt.Println("meow") #3
}
func NewCat() *Cat { #4
return &Cat{}
}
type Dog struct {}
func (d Dog) speak() {
fmt.Println("woof")
}
func NewDog() *Dog {
return &Dog{}
}
type Llama struct {}
func NewLlama() *Llama { #5
return &Llama{}
}
func main () {
var a Animal
c := NewCat()
a = c
a.speak()
d := NewDog()
a = d
a.speak()
l := NewLlama()
a = l
}
Shows an Animal interface, independent Cat, Dog, and Llama structs, methods, constructors, and interface-based dispatch. section 3.2
The approach replaces traditional class hierarchies with:
- Structs that represent object-like data. section 3.2
- Methods that attach data manipulation to types. section 3.2
- Interfaces that define precise behavioral requirements and expose missing implementations at compile time. section 3.2








