master
ZGGSONG 3 years ago
parent e42d830a83
commit 03fa1d4854

@ -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))
}

@ -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())
}

@ -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")
}

@ -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
}

@ -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
}
}

@ -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
}

@ -0,0 +1,52 @@
package main
import (
"fmt"
"strings"
)
/*
50Matthew,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
}

@ -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()
}
Loading…
Cancel
Save