Last active
December 9, 2021 03:12
-
-
Save monkrus/549895d99aafcb8e65ebb7258ea55cc9 to your computer and use it in GitHub Desktop.
Simplified explanation of interface in Golang
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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