乐趣区

关于golang: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<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
}
退出移动版