Skip to content

Instantly share code, notes, and snippets.

@mkock
Last active October 3, 2021 09:32
Show Gist options
  • Select an option

  • Save mkock/5a6f263005a7bc20d161e3618689c3aa to your computer and use it in GitHub Desktop.

Select an option

Save mkock/5a6f263005a7bc20d161e3618689c3aa to your computer and use it in GitHub Desktop.
type toaster interface {
toast()
}
type acmeToaster struct {}
func (a *acmeToaster) toast() { fmt.Println("Commencing toasting of bread...") }
func doToast(t toaster) {
t.toast()
}
func maybeDoToast(i interface{}) {
if t, ok := i.(toaster); ok {
t.toast()
}
}
func main() {
t := new(acmeToaster)
doToast(t) // Prints "Commencing toasting of bread..."
maybeDoToast(t) // Prints "Commencing toasting of bread..."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment