gpt4 book ai didi

阻止接收 channel

转载 作者:IT王子 更新时间:2023-10-29 02:14:49 25 4
gpt4 key购买 nike

我有以下 go 代码来等待流。预期的输出是:

line1
line2
line3
line4
line5
escape1
scan done
done....

但是在 line5 之后,我的代码一直挂起。

var lines = `
line1
line2
line3
line4
line5
line6
line7
`

func main() {
var (
donec = make(chan struct{})
stream = make(chan string, 5000)
exitc = make(chan struct{})
)
go func() {
scanner := bufio.NewScanner(strings.NewReader(lines))
escape1:
for {
for scanner.Scan() {
select {
case <-donec:
fmt.Println("escape1")
close(stream)
break escape1
default:
stream <- scanner.Text()
}
}
}
close(exitc)
fmt.Println("scan done")
return
}()

escape2:
for {
select {
case txt, ok := <-stream:
if !ok {
fmt.Println("stream closed!")
}
fmt.Println(txt)
if strings.Contains(txt, "line5") {
close(donec)
<-exitc
break escape2
}
}
}
fmt.Println("done....")
}

认为我做的一切都是对的。谁能帮我调试这个挂起的代码?

谢谢!

最佳答案

我认为这是因为您的 escape1 for 循环包装了 for scanner.Scan() 循环。

当我删除外部 for 循环时,它对我来说工作正常:https://play.golang.org/p/NU3m3Deil7

func main() {
var (
donec = make(chan struct{})
stream = make(chan string, 5000)
exitc = make(chan struct{})
)
go func() {
scanner := bufio.NewScanner(strings.NewReader(lines))
escape1:
for scanner.Scan() {
select {
case <-donec:
fmt.Println("escape1")
close(stream)
break escape1
default:
stream <- scanner.Text()
}
}

close(exitc)
fmt.Println("scan done")
return
}()

escape2:
for {
select {
case txt, ok := <-stream:
if !ok {
fmt.Println("stream closed!")
}
fmt.Println(txt)
if strings.Contains(txt, "line5") {
close(donec)
<-exitc
break escape2
}
}
}
fmt.Println("done....")
}

关于阻止接收 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36272673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com