package main
import (
"fmt"
"strings"
)
func AddIt() func (int) int {
var a = 10
return func(x int) int {
a = a + x
return a
}
}
//strings.HasSuffix(name,suff)
//判断name是否是suff的后缀
//如果是,返回name---否则返回name + 后缀
func makeSu(suff string) func (string) string{
return func(name string) string {
if !strings.HasSuffix(name,suff){
return name + suff;
}
return name
}
}
func main(){
//返回匿名函数
// f := AddIt()
// fmt.Println("f(1): ",f(1))
// fmt.Println("f(2): ",f(2))
// fmt.Println("f(3): ",f(3))
t := makeSu(".jpg")
fmt.Println("t(\"typ\"): ",t("typ"))
fmt.Println("t(\"oo.jpg\"): ",t("oo.jpg"))
}
Golang 闭包实现
最新推荐文章于 2023-02-27 13:27:33 发布