第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Go lang,通道處理序列

Go lang,通道處理序列

Go
慕碼人8056858 2021-11-01 16:33:10
我正在通過'A tour of Go'學(xué)習(xí)Go lang,很難理解Go channel運(yùn)行序列,package mainimport "fmt"import "time"func sum(a []int, c chan int) {    sum := 0    for _, v := range a {        time.Sleep(1000 * time.Millisecond)        sum += v    }    c <- sum // send sum to c}func main() {    a := []int{7, 2, 8, -9, 4, 0}    c := make(chan int)    go sum(a[:len(a)/2], c)    go sum(a[len(a)/2:], c)    x, y := <-c, <-c // receive from c    fmt.Println(x, y, x+y)    fmt.Println("Print this first,")}如果在代碼上方運(yùn)行,我預(yù)計(jì),Print this first,17 -5 12因?yàn)椋珿o 例程以非阻塞方式運(yùn)行,但是,實(shí)際上它打印,17 -5 12Print this first,我在網(wǎng)上找到的另一個(gè)例子,package mainimport "fmt"type Data struct {    i int}func func1(c chan *Data ) {    fmt.Println("Called")    for {        var t *Data;        t = <-c //receive        t.i += 10 //increment        c <- t   //send it back    }}func main() {    c := make(chan *Data)    t := Data{10}    go func1(c)    println(t.i)    c <- &t //send a pointer to our t    i := <-c //receive the result    println(i.i)    println(t.i)}此外,我預(yù)計(jì),它首先打印“調(diào)用”,但結(jié)果是102020Called我誤解了什么?請(qǐng)幫助我理解 Go 例程和通道。
查看完整描述

1 回答

  • 1 回答
  • 0 關(guān)注
  • 154 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)