Skip to content

Instantly share code, notes, and snippets.

@bertabus-zz
Created April 30, 2014 18:47
Show Gist options
  • Select an option

  • Save bertabus-zz/f9b93caf75a18a124fdb to your computer and use it in GitHub Desktop.

Select an option

Save bertabus-zz/f9b93caf75a18a124fdb to your computer and use it in GitHub Desktop.
First class functions in Go
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