1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
| package main
import ( "fmt" )
func FuncDemo5(arg1, arg2 float64) (float64, error) { if arg2 == 0 { return float64(0), fmt.Errorf("被除数不能为0") } else { return arg1/arg2, nil } }
func FuncDemo6() func() int { i := 0 return func() int { i++ return i } }
func FuncDemo7(arg1, arg2 int) func(arg3, arg4 int) (int, error) { i := 0 return func(arg3, arg4 int) (int, error) { i++ sum := arg1 + arg2 + arg3 + arg4 + i return sum, nil } }
func FuncDemo8(arg1, arg2 int) {
result := func(arg3, arg4 int) int { return arg1 * arg2 } r := result(arg1, arg2) fmt.Println(r)
}
func FuncDemo9(arg1, arg2 int) (int, error) { return arg1 + arg2, nil } func FuncDemo10(op func(arg1, arg2 int) (int, error), arg3, arg4 int) (int, error) { result, err := op(arg3, arg4) return result, err }
func FuncDemo11(numbers ...int) (int, error) { s := 0 for i := range numbers { s += numbers[i] } return s, nil }
func main() { if result, err := FuncDemo5(13,6); err != nil { fmt.Println("Error") } else { fmt.Println(result) }
if result, err := FuncDemo5(13,0); err != nil { fmt.Println("Error") } else { fmt.Println(result) }
nextValue := FuncDemo6() fmt.Println(nextValue()) fmt.Println(nextValue()) fmt.Println(nextValue())
nextValue2 := FuncDemo6() fmt.Println(nextValue2()) fmt.Println(nextValue2())
demo7 := FuncDemo7(5,9) fmt.Println(demo7(6,13)) fmt.Println(demo7(6,13)) fmt.Println(demo7(10,10))
FuncDemo8(5,6)
fmt.Println(FuncDemo10(FuncDemo9, 5,9))
fmt.Println(FuncDemo11(1,2,3,4,5)) }
|