Last active
October 3, 2021 09:32
-
-
Save mkock/5a6f263005a7bc20d161e3618689c3aa to your computer and use it in GitHub Desktop.
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
| 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