Skip to content

Instantly share code, notes, and snippets.

@SealOfTime
Created January 12, 2022 16:05
Show Gist options
  • Save SealOfTime/2cc7739395916f40ded1395ea7fabd5c to your computer and use it in GitHub Desktop.
Save SealOfTime/2cc7739395916f40ded1395ea7fabd5c to your computer and use it in GitHub Desktop.
Example of new interfaces in Go 1.18
//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