From 03fa1d4854d9c5ea4387edab417cf81e88eeb67d Mon Sep 17 00:00:00 2001 From: ZGGSONG Date: Wed, 9 Mar 2022 17:17:17 +0800 Subject: [PATCH] add func --- Func/closureDemo.go | 53 ++++++++++++++++++++++++++++++++++++++ Func/deferClassicalDemo.go | 38 +++++++++++++++++++++++++++ Func/deferDemo01.go | 11 ++++++++ Func/deferFace01.go | 18 +++++++++++++ Func/funcAsReturn.go | 27 +++++++++++++++++++ Func/test.go | 28 ++++++++++++++++++++ Practices/Func/fenjinbi.go | 52 +++++++++++++++++++++++++++++++++++++ expDemo/panicDemo.go | 29 +++++++++++++++++++++ 8 files changed, 256 insertions(+) create mode 100644 Func/closureDemo.go create mode 100644 Func/deferClassicalDemo.go create mode 100644 Func/deferDemo01.go create mode 100644 Func/deferFace01.go create mode 100644 Func/funcAsReturn.go create mode 100644 Func/test.go create mode 100644 Practices/Func/fenjinbi.go create mode 100644 expDemo/panicDemo.go diff --git a/Func/closureDemo.go b/Func/closureDemo.go new file mode 100644 index 0000000..8dc7eeb --- /dev/null +++ b/Func/closureDemo.go @@ -0,0 +1,53 @@ +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)) +} diff --git a/Func/deferClassicalDemo.go b/Func/deferClassicalDemo.go new file mode 100644 index 0000000..88ceaad --- /dev/null +++ b/Func/deferClassicalDemo.go @@ -0,0 +1,38 @@ +package main + +import "fmt" + +func f1() int { + x := 5 + defer func() { + x++ + }() + return x +} + +func f2() (x int) { + defer func() { + x++ + }() + return 5 +} + +func f3() (y int) { + x := 5 + defer func() { + x++ + }() + return x +} +func f4() (x int) { + defer func(x int) { + x++ + }(x) + return 5 +} +func main() { + fmt.Println(f1()) + fmt.Println(f2()) + fmt.Println(f3()) + fmt.Println(f4()) +} diff --git a/Func/deferDemo01.go b/Func/deferDemo01.go new file mode 100644 index 0000000..2702f7e --- /dev/null +++ b/Func/deferDemo01.go @@ -0,0 +1,11 @@ +package main + +import "fmt" + +func main() { + fmt.Println("start\n") + defer fmt.Println("1") + defer fmt.Println("2") + defer fmt.Println("3") + fmt.Printf("end\n") +} diff --git a/Func/deferFace01.go b/Func/deferFace01.go new file mode 100644 index 0000000..0f6d55a --- /dev/null +++ b/Func/deferFace01.go @@ -0,0 +1,18 @@ +package main + +import "fmt" + +func calc(index string, a, b int) int { + ret := a + b + fmt.Println(index, a, b, ret) + return ret +} + +func main() { + x := 1 + y := 2 + defer calc("AA", x, calc("A", x, y)) + x = 10 + defer calc("BB", x, calc("B", x, y)) + y = 20 +} diff --git a/Func/funcAsReturn.go b/Func/funcAsReturn.go new file mode 100644 index 0000000..5e107e8 --- /dev/null +++ b/Func/funcAsReturn.go @@ -0,0 +1,27 @@ +package main + +import "errors" + +func main() { + tt("*") +} + +func add(a, b int) int { + return a + b +} + +func sub(a, b int) int { + return a - b +} + +func tt(s string) (func(a, b int) int, error) { + switch s { + case "+": + return add, nil + case "-": + return sub, nil + default: + err := errors.New("输入错误") + return nil, err + } +} diff --git a/Func/test.go b/Func/test.go new file mode 100644 index 0000000..eeb264c --- /dev/null +++ b/Func/test.go @@ -0,0 +1,28 @@ +package main + +import "fmt" + +var temp int64 = 10 + +func main() { + fmt.Println(temp) + test() + fmt.Println(temp) + + s := "test2" + suffix := ".png" + res := test2(s, suffix) + fmt.Println(res) +} + +func test2(s, suffix string) bool { + t1 := len(s) >= len(suffix) + t2 := s[len(s)-len(suffix):] == suffix + temp := t1 && t2 + return temp +} + +func test() { + fmt.Println(temp) + temp = 100 +} diff --git a/Practices/Func/fenjinbi.go b/Practices/Func/fenjinbi.go new file mode 100644 index 0000000..8e45725 --- /dev/null +++ b/Practices/Func/fenjinbi.go @@ -0,0 +1,52 @@ +package main + +import ( + "fmt" + "strings" +) + +/* +你有50枚金币,需要分配给以下几个人:Matthew,Sarah,Augustus,Heidi,Emilie,Peter,Giana,Adriano,Aaron,Elizabeth。 +分配规则如下: +a. 名字中每包含1个'e'或'E'分1枚金币 +b. 名字中每包含1个'i'或'I'分2枚金币 +c. 名字中每包含1个'o'或'O'分3枚金币 +d: 名字中每包含1个'u'或'U'分4枚金币 +写一个程序,计算每个用户分到多少金币,以及最后剩余多少金币? +程序结构如下,请实现 ‘dispatchCoin’ 函数 +*/ +var ( + coins = 50 + users = []string{ + "Matthew", "Sarah", "Augustus", "Heidi", "Emilie", "Peter", "Giana", "Adriano", "Aaron", "Elizabeth", + } + distribution = make(map[string]int, len(users)) +) + +func main() { + left := dispatchCoin() + fmt.Println("剩下:", left) +} + +func dispatchCoin() interface{} { + for _, v := range users { + distribution[v] = 0 + } + for k := range distribution { + distribution[k] += strings.Count(k, "e") + distribution[k] += strings.Count(k, "E") + distribution[k] += strings.Count(k, "i") * 2 + distribution[k] += strings.Count(k, "I") * 2 + distribution[k] += strings.Count(k, "o") * 3 + distribution[k] += strings.Count(k, "O") * 3 + distribution[k] += strings.Count(k, "u") * 4 + distribution[k] += strings.Count(k, "U") * 4 + //fmt.Println(k, distribution[k]) + } + sum := 0 + for _, v := range distribution { + sum += v + } + + return coins - sum +} diff --git a/expDemo/panicDemo.go b/expDemo/panicDemo.go new file mode 100644 index 0000000..e44afdc --- /dev/null +++ b/expDemo/panicDemo.go @@ -0,0 +1,29 @@ +package main + +import "fmt" + +func funcA() { + fmt.Println("func A") +} + +func funcB() { + //recover接受panic抛出的异常 + //recover只能在defer函数中调用才会生效 + //defer一定要在可能引发panic的语句之前定义 + defer func() { + err := recover() + if err != nil { + fmt.Println("recover in B") + } + }() + panic("panic in B") +} + +func funcC() { + fmt.Println("func C") +} +func main() { + funcA() + funcB() + funcC() +}