原来Go2.0的泛型是这样的...

前端之家收集整理的这篇文章主要介绍了原来Go2.0的泛型是这样的...前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
func main() {
    var jobs = Chan<Int>(buffer: 5)
    var done = Chan<Bool>()

    go {
        for ;; {
            var (j,more) = <?jobs
            if more {
                println("received job \(j!)")
            } else {
                println("received all jobs")
                done <- true
                return
            }
        }
    }

    for var j = 1; j <= 3; j++ {
        jobs <- j
        println("sent job \(j)")
    }
    close(jobs)
    println("sent all jobs")

    <-done
}

猜你在找的Swift相关文章