liveBook · Manning

Object-oriented programming

As explained in Go in Practice, Second Edition

Object-oriented programming is a programming paradigm that organizes software around objects combining data with operations and commonly uses abstraction, inheritance, and polymorphism.

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

How Go in Practice, Second Edition covers it

How deep it goes
Explains the model through Java and Go listings, then demonstrates compile-time interface checking.
Examples
Uses Animal, Cat, Dog, and Llama examples to contrast inheritance with interface relationships.
What it assumes
Assumes familiarity with object-oriented abstractions while introducing the corresponding Go mechanisms.
What you can do after
Apply it via Go structs, methods, and interfaces to model object-like behavior without explicit inheritance.
How it connects
Connects structs, interfaces, methods, inheritance, and polymorphism across Java and Go.

Also explained in

Compare how 9 books explain object-oriented programming →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.