You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
54 lines
777 B
54 lines
777 B
3 years ago
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
)
|
||
|
|
||
|
//闭包
|
||
|
func adder(x int) func(int) int {
|
||
|
return func(y int) int {
|
||
|
x += y
|
||
|
return x
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func suffixFunc(suffix string) func(string) string {
|
||
|
return func(name string) string {
|
||
|
if !strings.HasSuffix(name, suffix) {
|
||
|
return name + suffix
|
||
|
}
|
||
|
return name
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func cal(base int) (func(int) int, func(int) int) {
|
||
|
add := func(i int) int {
|
||
|
base += i
|
||
|
return base
|
||
|
}
|
||
|
|
||
|
sub := func(i int) int {
|
||
|
base -= i
|
||
|
return base
|
||
|
}
|
||
|
return add, sub
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
a, b := cal(10)
|
||
|
fmt.Println(a(20), b(30))
|
||
|
fmt.Println(a(100), b(80))
|
||
|
|
||
|
//jpgFunc := suffixFunc(".jpg")
|
||
|
//res := jpgFunc("test.jpg")
|
||
|
//fmt.Println(res)
|
||
|
|
||
|
//a := adder(10)
|
||
|
//fmt.Println(a(20))
|
||
|
//fmt.Println(a(40))
|
||
|
//
|
||
|
//b := adder(20)
|
||
|
//fmt.Println(b(40))
|
||
|
}
|