golang 笔记

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", v.Name,v.Age)
    }
}

2.chan作为互斥锁


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

你可能感兴趣的:(golang)