Methods

Methods are functions that have a receiver (see ).You can define methods on any type (except on non-local types, this includesbuilt-in types: the type int can not have methods).You can however make a new integer type with its own methods. For example:

  1. type Foo int
  2. func (self Foo) Emit() {
  3. fmt.Printf("%v", self)
  4. }
  5. type Emitter interface {
  6. Emit()
  7. }

Doing this on non-local (types defined in other packages) types yields an error“cannot define new methods on non-local type int”.