douzhang7184 2017-09-13 00:42
浏览 30
已采纳

GO程序陷入循环

// _Closing_ a channel indicates that no more values
// will be sent on it. This can be useful to communicate
// completion to the channel's receivers.

package main

import "fmt"

// In this example we'll use a `jobs` channel to
// communicate work to be done from the `main()` goroutine
// to a worker goroutine. When we have no more jobs for
// the worker we'll `close` the `jobs` channel.
func main() {
    jobs := make(chan int, 5)
    done := make(chan bool)

    // Here's the worker goroutine. It repeatedly receives
    // from `jobs` with `j, more := <-jobs`. In this
    // special 2-value form of receive, the `more` value
    // will be `false` if `jobs` has been `close`d and all
    // values in the channel have already been received.
    // We use this to notify on `done` when we've worked
    // all our jobs.

    for i := 1; i <= 3; i++ {
        go func() {
            for {
                j, more := <-jobs
                if more {
                    fmt.Println("received job", j)
                } else {
                    fmt.Println("received all jobs")
                    done <- true
                    return
                }
            }
        }()
    }

    // This sends 3 jobs to the worker over the `jobs`
    // channel, then closes it.
    j := 0
    for {
        j++
        jobs <- j
        fmt.Println("sent job", j)
    }
    close(jobs)
    fmt.Println("sent all jobs")

    // We await the worker using the
    // [synchronization](channel-synchronization) approach
    // we saw earlier.
    <-done
}

https://play.golang.org/p/x28R_g8ftS

What I'm trying to do is get all the responses from a paginated url endpoint. jobs is a channel storing the page number. I have a function in if more{} checking for empty reponse and I have

done <- true
return

I thought this would close the go routine. But, the page generator for{j++; jobs <- j} is causing it to get stuck in a loop. Any idea how this can be resolved?

  • 写回答

3条回答 默认 最新

  • douzhu6149 2017-09-16 00:44
    关注

    This was I was looking for. I have a number generator in an infinite while loop. And the program exits on some condition, in this example, it is on the j value, but it can also be something else.

    https://play.golang.org/p/Ud4etTjrmx

    package main
    
    import "fmt"
    
    
    
    func jobs(job chan int) {
        i := 1
        for {
            job <- i
            i++
        }
    }
    
    func main() {
        jobsChan := make(chan int, 5)
    
    
        done := false
        j := 0
    
        go jobs(jobsChan)
        for !done {
            j = <-jobsChan
            if j < 20 {
                fmt.Printf("job %d
    ", j)
            } else {
                done = true
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分