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

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

我可以重新打開已關(guān)閉的頻道嗎?

我可以重新打開已關(guān)閉的頻道嗎?

Go
慕村225694 2023-04-24 16:46:20
我想弄清楚是否可以在關(guān)閉頻道后重新打開它。測試用例:我有一個頻道,里面有一些東西我想覆蓋它們,因此我需要事先關(guān)閉頻道我想在頻道中放入更多內(nèi)容并再次遍歷它go func() {    queue <- "1"    queue <- "2"    close(queue)}()for i := range queue {    go func(i string) {        fmt.Println("From queue: ", i)    }(i)}go func() {    open(queue)    queue <- "3"    queue <- "4"    close(queue)}()for i := range queue {    go func(i string) {        fmt.Println("From queue: ", i)    }(i)}當(dāng)然open不存在。我怎樣才能在 Go 中實現(xiàn)我需要的東西?我不想使用睡眠功能
查看完整描述

3 回答

?
紫衣仙女

TA貢獻(xiàn)1839條經(jīng)驗 獲得超15個贊

我想覆蓋它們,因此我需要事先關(guān)閉頻道


不,不需要關(guān)閉頻道。當(dāng)另一個項目通過通道推送時,它將恢復(fù)迭代。


下面的代碼接受控制臺輸入并將其推送到頻道:


主程序


package main


import (

    "log"

    "bufio"

    "os"

    "fmt"

)


func process(c chan string) {

    for s := range c {

        log.Println("processed", s)

    }

}


func main() {

    c := make(chan string, 10)

    go process(c)


    // get from console and process

    reader := bufio.NewReader(os.Stdin)

    fmt.Println("INPUT STUFF. TYPE #quit TO EXIT.")

    for {

        input, _, _ := reader.ReadLine()

        if string(input) == "#quit" {

            break

        }

        c <- string(input)

    }


    log.Println("BYE!")

}

輸出


INPUT STUFF. TYPE #quit TO EXIT.

hello

2018/10/23 10:43:52 processed hello

world

2018/10/23 10:43:54 processed world

#quit

2018/10/23 10:43:57 BYE!

下面的示例使用并可作為 Go Playground片段Sleep()運行


package main


import (

    "log"

    "time"

)


func process(c chan string) {

    for s := range c {

        log.Println("processed", s)

    }

}


func main() {

    c := make(chan string, 10)


    go process(c)


    // push some data

    c <- "barry allen"

    c <- "iris west"


    time.Sleep(time.Second * 2)


    // push more data

    c <- "joe west"

    c <- "caitlin snow"


    time.Sleep(time.Second * 3)

}

輸出


2009/11/10 23:00:00 processed barry allen

2009/11/10 23:00:00 processed iris west

2009/11/10 23:00:02 processed joe west

2009/11/10 23:00:02 processed caitlin snow

希望這可以幫助。干杯,


查看完整回答
反對 回復(fù) 2023-04-24
?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗 獲得超4個贊

您不能重新打開已關(guān)閉的頻道,但可以channel在頻道上發(fā)送,也許這就是您要找的?

package main


import (

? ? "fmt"

? ? "time"

)


func main() {

? ? queue := make(chan chan int)

? ? defer close(queue)


? ? go func() { // reader

? ? ? ? for {

? ? ? ? ? ? ch := <-queue

? ? ? ? ? ? for i := range ch {

? ? ? ? ? ? ? ? fmt.Println(i)

? ? ? ? ? ? }

? ? ? ? ? ? fmt.Println("Done with this channel")

? ? ? ? }

? ? }()


? ? go func() { // writer-1

? ? ? ? ch := make(chan int)

? ? ? ? defer close(ch)

? ? ? ? queue <- ch

? ? ? ? ch <- 4

? ? ? ? ch <- 2

? ? }()


? ? go func() { // writer-2

? ? ? ? ch := make(chan int)

? ? ? ? defer close(ch)

? ? ? ? queue <- ch

? ? ? ? ch <- 4

? ? ? ? ch <- 20

? ? }()

? ? time.Sleep(time.Second)

}


查看完整回答
反對 回復(fù) 2023-04-24
?
翻閱古今

TA貢獻(xiàn)1780條經(jīng)驗 獲得超5個贊

雖然您無法重新打開頻道,但可以創(chuàng)建一個新頻道并分配給變量。先前關(guān)閉的通道將被垃圾收集。在本例中,我重用了該queue變量,但您也可以創(chuàng)建一個新queue2變量并將其傳遞給 goroutine。


package main


import (

    "context"

    "fmt"

    "time"

)


func main() {

    ctx := context.Background()

    ctx, cancel := context.WithCancel(ctx)

    queue := make(chan int)

    go printFormat(nil, queue)

    queue <- 1

    queue <- 2

    close(queue)

    // fake some actions in between

    time.Sleep(2 * time.Second)

    queue = make(chan int)

    go printFormat(cancel, queue)

    queue <- 3

    queue <- 4

    close(queue)

    <-ctx.Done()

}


func printFormat(c context.CancelFunc, q chan int) {

    for i := range q {

        fmt.Printf("Data %d \n", i)

    }

    if c != nil {

        c()

    }

}


查看完整回答
反對 回復(fù) 2023-04-24
  • 3 回答
  • 0 關(guān)注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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