Skip to content

Instantly share code, notes, and snippets.

@baisong
Created March 9, 2012 20:31
Show Gist options
  • Save baisong/2008511 to your computer and use it in GitHub Desktop.
Save baisong/2008511 to your computer and use it in GitHub Desktop.
gosofunky mandarin chinese translation
/*
* gosofunky - demos 3 'functional programming' ways to use functions in go
*/
package main
import "fmt"
/**
* Way #1: 函数当作输入(引数)和输出(传出值)
*
* To demo this, let's define a go [:type:] to act as a passed function.
* A [:传函类:] takes no arguments and returns a string.
*/
type 传函类 func() string
/**
* Way #1 (cont'd)
*
* Here's what it looks like when we instantiate a [:传函类:] type:
* [:foo:] is a 传函类.
*/
func 全局传函() string {
return "我是【全局传函】(传函类)的传出值(string类)。"
}
/**
* Way #1 (cont'd)
*
* Here's the function we're going to pass [:foo:] to later on:
* [:takes传函类:] takes one 传函类 as a parameter.
*/
func 引传函类(局部传函 传函类) {
fmt.Printf("引传函类:%v\n", 局部传函())
}
/**
* Way #2: functions returning functions
*
* Here's a function that returns a function.
* [:returns传函类:] returns a 传函类.
*/
func 出传函类() 传函类 {
return func() string {
fmt.Printf("出传函类\n")
return "I am the returns传函类 return value."
}
}
func main() {
fmt.Printf("[1] takes传函类(foo)\n")
// Must not pass [:foo:] as [:foo():] -- that would be a string.
takes传函类(foo)
fmt.Printf("[2] returns传函类()\n")
var f 传函类 = returns传函类()
// Must not pass [:f:] as [:f():] -- that would be a string.
takes传函类(f)
/**
* Way #3 storing an anonymous function in a variable
*/
fmt.Printf("[3] stored anonymous 传函类\n")
var storeAnon 传函类 = func() string {
return "I am the anonymous 传函类\n"
}
// Must not pass [:storeAnon():] as [:storeAnon:] -- that would be a 传函类!
fmt.Printf(storeAnon())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment