英文:
Go blocking from receiving channel
问题
我有以下的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....")
}
我认为我做得没错。有人可以帮我调试这个挂起的代码吗?
谢谢!
英文:
I have the following go code to wait for streams. The expected output is:
line1
line2
line3
line4
line5
escape1
scan done
done....
But after line5
, my code keeps hanging.
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....")
}
Think I am doing everything right. Could anybody help me debug this hanging code?
Thanks!
答案1
得分: 1
我认为问题出在你的 escape1
循环包裹了 for scanner.Scan()
循环。
当我移除外部的循环后,代码在我这里可以正常工作: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....")
}
请注意,我只是翻译了你的代码,并没有回答你的问题。
英文:
I think it's because your escape1
for loop wraps the for scanner.Scan()
loop.
When I remove that outer for loop it works fine for me: 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....")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论