Skip to content

Instantly share code, notes, and snippets.

@monkrus
Last active December 9, 2021 03:12
Show Gist options
  • Select an option

  • Save monkrus/549895d99aafcb8e65ebb7258ea55cc9 to your computer and use it in GitHub Desktop.

Select an option

Save monkrus/549895d99aafcb8e65ebb7258ea55cc9 to your computer and use it in GitHub Desktop.
Simplified explanation of interface in Golang
package main
import (
"fmt"
)
// INTERFACE IS A ACTION
type marathon interface {
satisfaction() int
}
// STRUCT IS A PERSON WHO DOES ACTION. DESCRIBES THE QUALITIES OF THE PERSON.
type runner struct {
endurance, persistence int
}
//FUNC SHOWS A WAY TO PERFORM ACTION
func (r runner) satisfaction() int {
return r.endurance + r.persistence
}
// HELPER FUNCTION MEASURES THE ACTION
func measure(m marathon) {
fmt.Println(m)
fmt.Println(m.satisfaction())
}
func main() {
m := runner{endurance: 5, persistence: 8}
measure(m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment