本文摘自网络,作者Atticus,侵删。
package main
import (
"fmt"
"strconv"
)
type Attach func(string) string
func (a Attach) loopTimes(n int) string {
for i := 1; i < n; i ++ {
a(strconv.Itoa(i))
}
return a(strconv.Itoa(n))
}
func newAttach() Attach {
basic := "Begin"
return func(s string) string {
basic = fmt.Sprintf("%s\n%s", basic, s)
return basic
}
}
func main() {
a1 := newAttach()
fmt.Println(a1.loopTimes(10))
}
输出是:
Begin
1
2
3
4
5
6
7
8
9
10
你理解了么?
相关阅读 >>
kubernetes学习笔记之csi external provisioner源码解析
用 wasmedge 中的嵌入式 webassembly 函数扩展 Golang 应用程序
你知道为啥不再建议使用Gopath以及如何使用Go modules吗
更多相关阅读请进入《Go》频道 >>

Go语言101
一个与时俱进的Go编程知识库。