阻止接收频道

时间:2016-03-28 23:03:36

标签: go concurrency 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....")
}

认为我做的一切都是正确的。有人可以帮我调试这个挂码吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

我认为这是因为你的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....")
}