Skip to content

Latest commit

 

History

History
42 lines (29 loc) · 852 Bytes

methods.md

File metadata and controls

42 lines (29 loc) · 852 Bytes

Overview

  1. Key features of methods
  2. See also Functions doc

Methods overview

  1. A function attached to a type
  2. Almost anything can have methods (attached functions)
    1. structs (obvious), numbers, functions, channels
    2. CANNOT attach to interfaces
  3. Method attached to a function allow decorating, proxying, ...
  4. Every type has a (possibly empty) method set associated
  • TODO: mention "receiver"

Intuition

  1. Think of it like passing the receiver as first arg
type Car struct {
}

func (c Car) DoFoo(arg int) int {
    // ...
}

car := Car{}
_ = car.DoFoo(7)

// equivalent to:  GenerateDoFoo(car, 7)

TODO: pointer receiver TODO: non-pointer receiver

Idioms

TODO ...

Other Resources

  1. https://www.practical-go-lessons.com/chap-14-methods