Created
January 12, 2022 16:05
-
-
Save SealOfTime/2cc7739395916f40ded1395ea7fabd5c to your computer and use it in GitHub Desktop.
Example of new interfaces in Go 1.18
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
//Forbid me for the ugly formatting, this is a neccessary loss for saving some visual space in the article. | |
type Pet interface { | |
Dog | Cat | |
Say() string | |
} | |
type Dog struct{} | |
func (d Dog) Say() string { return "I'm an incredibly intelligent dog" } | |
type Cat struct{} | |
func (c Cat) Say() string { return "Meow :/" } | |
type Fox struct{} | |
func (f Fox) Say() string { return "2013 was dope" } | |
func playWith[P Pet](pet P) { fmt.Printf("My pet is happy and says: %s\n", pet.Say()) } | |
func main() { | |
playWith(Dog{}) | |
playWith(Cat{}) | |
playWith(Fox{}) //./prog.go:21:10: Fox does not implement Pet | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment