1.foreach

for循环是对值的拷贝

type student struct {
    Name string
    Age int
}
func forDemo() {
    m := make(map[string] * student)
    stus := [] student {
        {
            Name: "zhang",
            Age: 24,
        }, {
            Name: "li",
            Age: 23,
        }, {
            Name: "zhao",
            Age: 22,
        },
    }

    for i := 0; i < len(stus); i++ {
        stus[i].Age = stus[i].Age + 10 //这样操作
    }

    for i := 0;i<len(stus);i++ {
        m[stus[i].Name] = & stus[i]  //这样操作
    }

    for k, v := range m {
        println(k, "=>", v.Name,v.Age)
    }
}

2.chan作为互斥锁


func add(h chan int, wg *sync.WaitGroup) {
    defer wg.Done()
    h <- 1    
    num += 1
    <-h
}

你若安好便是晴天
82 声望10 粉丝