Created
February 17, 2021 15:31
-
-
Save lovemapa/d7edb9ac93f81368fdf797887ab5dc86 to your computer and use it in GitHub Desktop.
Go interfaces
This file contains 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 interface | |
import "fmt" | |
type Animal interface { | |
makeSound() string | |
} | |
type Goat struct{} | |
type Dog struct{} | |
func main() { | |
g := Goat{} | |
d := Dog{} | |
printVoice(g) | |
printVoice(d) | |
} | |
func printVoice(a Animal) { | |
fmt.Println(a.makeSound()) | |
} | |
func (Goat) makeSound() string { | |
return "Bleats" | |
} | |
func (Dog) makeSound() string { | |
return "Barks" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment