9

大佬帮忙看下 channel 的问题,多个协程执行

 1 year ago
source link: https://studygolang.com/articles/36041
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

大佬帮忙看下 channel 的问题,多个协程执行

harryin777 · 大约2小时之前 · 82 次点击 · 预计阅读时间 1 分钟 · 大约8小时之前 开始浏览    

type query func(string) string

func exec(name string, vs ...query) string {
    ch := make(chan string)
    fn := func(i int) {
        fmt.Printf("here : %v \n", i)
        ch <- vs[i](name)
    }
    fmt.Println(len(vs))
    for i, _ := range vs {
        go fn(i)
    }
    return <-ch
}

func Test10(t *testing.T) {
    ret := exec("111", func(n string) string {
        return n + "func1"
    }, func(n string) string {
        return n + "func2"
    }, func(n string) string {
        return n + "func3"
    }, func(n string) string {
        return n + "func4"
    })

    fmt.Println(ret)
}

为什么上面的代码一直都是输出 111func4。 其他的为什么不输出?


有疑问加站长微信联系(非本文作者))

280

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK