猿问

我可以重新打开已关闭的频道吗?

我想弄清楚是否可以在关闭频道后重新打开它。

测试用例:

  1. 我有一个频道,里面有一些东西

  2. 我想覆盖它们,因此我需要事先关闭频道

  3. 我想在频道中放入更多内容并再次遍历它

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)

}

当然open不存在。我怎样才能在 Go 中实现我需要的东西?

我不想使用睡眠功能


慕村225694
浏览 108回答 3
3回答

紫衣仙女

我想覆盖它们,因此我需要事先关闭频道不,不需要关闭频道。当另一个项目通过通道推送时,它将恢复迭代。下面的代码接受控制台输入并将其推送到频道:主程序package mainimport (&nbsp; &nbsp; "log"&nbsp; &nbsp; "bufio"&nbsp; &nbsp; "os"&nbsp; &nbsp; "fmt")func process(c chan string) {&nbsp; &nbsp; for s := range c {&nbsp; &nbsp; &nbsp; &nbsp; log.Println("processed", s)&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; c := make(chan string, 10)&nbsp; &nbsp; go process(c)&nbsp; &nbsp; // get from console and process&nbsp; &nbsp; reader := bufio.NewReader(os.Stdin)&nbsp; &nbsp; fmt.Println("INPUT STUFF. TYPE #quit TO EXIT.")&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; input, _, _ := reader.ReadLine()&nbsp; &nbsp; &nbsp; &nbsp; if string(input) == "#quit" {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; c <- string(input)&nbsp; &nbsp; }&nbsp; &nbsp; log.Println("BYE!")}输出INPUT STUFF. TYPE #quit TO EXIT.hello2018/10/23 10:43:52 processed helloworld2018/10/23 10:43:54 processed world#quit2018/10/23 10:43:57 BYE!下面的示例使用并可作为 Go Playground片段Sleep()运行package mainimport (&nbsp; &nbsp; "log"&nbsp; &nbsp; "time")func process(c chan string) {&nbsp; &nbsp; for s := range c {&nbsp; &nbsp; &nbsp; &nbsp; log.Println("processed", s)&nbsp; &nbsp; }}func main() {&nbsp; &nbsp; c := make(chan string, 10)&nbsp; &nbsp; go process(c)&nbsp; &nbsp; // push some data&nbsp; &nbsp; c <- "barry allen"&nbsp; &nbsp; c <- "iris west"&nbsp; &nbsp; time.Sleep(time.Second * 2)&nbsp; &nbsp; // push more data&nbsp; &nbsp; c <- "joe west"&nbsp; &nbsp; c <- "caitlin snow"&nbsp; &nbsp; time.Sleep(time.Second * 3)}输出2009/11/10 23:00:00 processed barry allen2009/11/10 23:00:00 processed iris west2009/11/10 23:00:02 processed joe west2009/11/10 23:00:02 processed caitlin snow希望这可以帮助。干杯,

慕莱坞森

您不能重新打开已关闭的频道,但可以channel在频道上发送,也许这就是您要找的?package mainimport (    "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)}

翻阅古今

虽然您无法重新打开频道,但可以创建一个新频道并分配给变量。先前关闭的通道将被垃圾收集。在本例中,我重用了该queue变量,但您也可以创建一个新queue2变量并将其传递给 goroutine。package mainimport (&nbsp; &nbsp; "context"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "time")func main() {&nbsp; &nbsp; ctx := context.Background()&nbsp; &nbsp; ctx, cancel := context.WithCancel(ctx)&nbsp; &nbsp; queue := make(chan int)&nbsp; &nbsp; go printFormat(nil, queue)&nbsp; &nbsp; queue <- 1&nbsp; &nbsp; queue <- 2&nbsp; &nbsp; close(queue)&nbsp; &nbsp; // fake some actions in between&nbsp; &nbsp; time.Sleep(2 * time.Second)&nbsp; &nbsp; queue = make(chan int)&nbsp; &nbsp; go printFormat(cancel, queue)&nbsp; &nbsp; queue <- 3&nbsp; &nbsp; queue <- 4&nbsp; &nbsp; close(queue)&nbsp; &nbsp; <-ctx.Done()}func printFormat(c context.CancelFunc, q chan int) {&nbsp; &nbsp; for i := range q {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("Data %d \n", i)&nbsp; &nbsp; }&nbsp; &nbsp; if c != nil {&nbsp; &nbsp; &nbsp; &nbsp; c()&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Go
我要回答