一、函數閉包
package main
import "fmt"
func adder() func(int) int { //函數作爲返回值
sum := 0//自由變量,每次調用都保留上次的結果
return func(v int) int {//v是局部變量
sum += v
return sum
}
}
func Fibonacci() func() int {
a, b := 0, 1
return func() int {
a, b = b, a+b
return a
}
}
func main() {
// a := adder() is trivial and also works.
a := adder()
for i := 0; i < 10; i++ {
var s int
s = a(i)
fmt.Printf("0 + 1 + ... + %d = %d\n",
i, s)
}
f := Fibonacci()
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
fmt.Println(f())
}
輸出:
0 + 1 + ... + 0 = 0
0 + 1 + ... + 1 = 1
0 + 1 + ... + 2 = 3
0 + 1 + ... + 3 = 6
0 + 1 + ... + 4 = 10
0 + 1 + ... + 5 = 15
0 + 1 + ... + 6 = 21
0 + 1 + ... + 7 = 28
0 + 1 + ... + 8 = 36
0 + 1 + ... + 9 = 45
1
1
2
3
5
8
13
21
34
55
89
二、函數可以實現接口
package main
import (
"bufio"
"fmt"
"io"
"strings"
)
func Fibonacci() func() int {
a, b := 0, 1
return func() int {
a, b = b, a+b
return a
}
}
type intGen func() int
func (g intGen) Read(
p []byte) (n int, err error) {//函數實現了接口
next := g()
if next > 10000 {
return 0, io.EOF
}
s := fmt.Sprintf("%d\n", next)
// TODO: incorrect if p is too small!
return strings.NewReader(s).Read(p)
}
func printFileContents(reader io.Reader) {//會調用上面被實現的Read函數
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
}
func main() {
var f intGen = Fibonacci()
printFileContents(f)
}
二、函數作爲參數和返回值
package main
import (
"net/http"
_ "net/http/pprof"
)
type appHandler func(writer http.ResponseWriter,
request *http.Request) error
func errWrapper(
handler appHandler) func(
http.ResponseWriter, *http.Request) {//函數作爲參數和返回值
return func(writer http.ResponseWriter,
request *http.Request) {
handler(writer, request)
}
}
func HandleFileList(writer http.ResponseWriter,
request *http.Request) error {
return nil
}
func main() {
http.HandleFunc("/",
errWrapper(HandleFileList))
err := http.ListenAndServe(":8888", nil)
if err != nil {
panic(err)
}
}