Created
April 30, 2014 18:47
-
-
Save bertabus-zz/f9b93caf75a18a124fdb to your computer and use it in GitHub Desktop.
First class functions in Go
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
| package main | |
| import fmt "fmt" | |
| type Stringy func() string | |
| func foo() string { | |
| return "Stringy function" | |
| } | |
| func takesAFunction(foo Stringy) { | |
| fmt.Printf("takesAFunction: %v\n", foo()) | |
| } | |
| func returnsAFunction() Stringy { | |
| return func() string { | |
| fmt.Printf("Inner stringy function\n") | |
| return "bar" // have to return a string to be stringy | |
| } | |
| } | |
| func main() { | |
| takesAFunction(foo) | |
| var f Stringy = returnsAFunction() | |
| f() | |
| var baz Stringy = func() string { return "anonymous stringy\n" } | |
| fmt.Printf(baz()) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment