listen6_函数详解

1.变量作⽤域和可⻅性

2. 匿名函数 

3. 闭包

4. 课后练习

 

1 全局变量,在程序整个⽣命周期有效

2. 局部变量,分为两种:1)函数内定义,2)语句块内定义(for 循环 内变量)

3. 可⻅性,包内任何变量或函数都是能访问的。包外的话,⾸字⺟⼤写是可导出的 能够被其他包访问或调⽤。⼩写表示是私有的,不能被外部的包访问

 

func testFunc2() {
    f1 := func(a, b int) int { //匿名函数
        return a + b
    }
    fmt.Printf("type of t1 %T\n", f1)
    sum := f1(1, 5)
    fmt.Printf("sum=%d\n", sum)
}

func testFunc4() {
    var i int = 0
    defer func() {
        fmt.Printf("defer i=%d\n", i)
    }()
    i = 100
    fmt.Printf("i=%d\n", i)
    return
}

 

匿名函数

1. 函数也是⼀种类型,因此可以定义⼀个函数类型的变量

2 .匿名函数,即没有名字的函数

3 defer中使⽤匿名函数

4. 函数作为⼀个参数

1. 闭包:⼀个函数和与其相关的引⽤环境组合⽽成的实体

 

func Adder() func(int) int {
    var x int
    return func(d int) int {
        x += d
        return x
    }
}

func add(base int) func(int) int {
    return func(i int) int {
        base += 1
        return base
    }
}

func testClouser2() {
    tmp1 := add(10)
    fmt.Println(tmp1(1), tmp1(2))
    tmp2 := add(100)
    fmt.Println(tmp2(1), tmp2(2))

}

func testClosure1() {
    f := Adder()
    ret := f(1)

    fmt.Printf("f(1):ret=%d\n", ret)
    ret = f(20)
    fmt.Printf("f(20):ret=%d\n", ret)
    ret = f(100)
    fmt.Printf("f(100):ret%d\n", ret)

    f1 := Adder()
    ret = f1(1)
    fmt.Printf("F(1):RET=%d\n", ret)
    ret = f1(1000)
    fmt.Printf("f(1000);ret=%d\n", ret)

}

func makeSuffiFunc(suffix string) func(string) string {
    return func(name string) string {
        if !strings.HasSuffix(name, suffix) {
            return name + suffix
        }
        return name
    }
}
func testClosure3() {
    func1 := makeSuffiFunc(".bmp")
    func2 := makeSuffiFunc(".jpg")
    fmt.Println(func1("test.bmp"))
    fmt.Println(func2("tessdft"))
}

func calc(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 testClosure4() {

    f1, f2 := calc(10)
    fmt.Println(f1(1), f2(6))
    fmt.Println(f1(3), f2(6))
    fmt.Println(f1(5), f2(6))
}

func testClosure5() {
    for i := 0; i < 5; i++ {
        go func(index int) { //这里为什么传个index
            fmt.Println(index)
        }(i)
    }
    time.Sleep(time.Second)
}
View Code

 

 sort  排序

package main

import "fmt"

func insert_sort(a [8]int) [8]int { //插入排序
    for i := 1; i < len(a); i++ {
        for j := i; j > 0; j-- {
            if a[j] < a[j-1] {
                a[j], a[j-1] = a[j-1], a[j]
            } else {
                break
            }

        }
    }
    return a
}

//选择排序
func select_sort(a [8]int) [8]int {
    for i := 0; i < len(a); i++ {
        for j := i; j < len(a); j++ {
            if a[j] < a[i] {
                a[i], a[j] = a[j], a[i]
            }
        }
    }
    return a

}

// func bubble_sort(a [8]int) [8]int {
//     for i := 0; i < len(a); i++ {
//         for j := 0; j < len(a)-i-1; j++ {
//             if a[j] > a[j+1] {
//                 a[j], a[j+1] = a[j+1], a[j]
//             }
//         }
//     }
//     return a
// }

func bubble_sort(a [8]int) [8]int {
    for i := 0; i < len(a); i++ {
        for j := 0; j < len(a)-i-i; j++ {
            if a[j] > a[j+1] {
                a[j], a[j+1] = a[j+1], a[j]
            }
        }
    }
    return a
}

func main() {
    var i [8]int = [8]int{2, 5, 8, 9, 6, 3, 4, 7}
    // fmt.Print(insert_sort(i))
    fmt.Print(select_sort(i))

}

 

 

1. 实现⼀个插⼊排序

2.实现⼀个选择排序 练习

3. 实现⼀个冒泡排序

 

func testFunc2() {
    f1 := func(a, b int) int { //匿名函数
        return a + b
    }
    fmt.Printf("type of t1 %T\n", f1)
    sum := f1(1, 5)
    fmt.Printf("sum=%d\n", sum)
}

func testFunc4() {
    var i int = 0
    defer func() {
        fmt.Printf("defer i=%d\n", i) //这是调用的时候赋值的  i为100
    }()
    i = 100
    fmt.Printf("i=%d\n", i)
    return
}

func sub(a, b int) int {
    return a - b
}

func testC(a, b int32) int32 {
    return a * b
}
func calc(a, b int, op func(int, int) int) int { //opt  匿名函数
    return op(a, b)

 

posted @ 2022-04-02 20:48  walkerpython  阅读(28)  评论(0编辑  收藏  举报