Sunday, May 2, 2021

Receivers

d is the instance. Similar to self in python,



Convention of instance is normally lowercase letters.

When to use a receiver?

  • It depends
  • You need to determine firsr the function signature
  • For example, if you want to execute instance.print() you must use a receiver
func (d deck) print() {
    for i, card := range d {
        fmt.Println(i, card)
    }
}

Other format

If the instance will not be used.


func (eb englishBot) getGreeting() string {
    return "Hi there!"
}


You can ommit the variable and do it this way.


func (englishBot) getGreeting() string {
    return "Hi there!"
}

No comments:

Post a Comment