package main
import (
"fmt"
"strings"
)
func makeSuffixFunc(suffix string) func(string) string {
return func(name string) string {
if !strings.HasSuffix(name, suffix) {
return name + suffix
}
return name
}
}
func main() {
func1 := makeSuffixFunc(".bmp")
func2 := makeSuffixFunc(".jpg")
fmt.Println(func1, func2)
fmt.Println(func1("hello12.bmp"))
fmt.Println(func2("name"))
}
输出:
0x29b9c0 0x29b920
hello12.bmp
name.jpg
标签:闭包,func2,string,fmt,golang,suffix,func,装饰,name
From: https://www.cnblogs.com/heris/p/17476852.html