2017-09-13 41 views
-1
// _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_g8ftSGO程序停留在循環

我想要做的就是從分頁URL端點所有響應。作業是存儲頁碼的渠道。我有一個功能,如果更多{}檢查空響應,我有

done <- true 
return 

我認爲這將關閉去例行。 但是,頁面生成器for{j++; jobs <- j}正在導致它陷入循環。任何想法如何解決這個問題?

+0

在什麼時候,你會想到的是循環退出?它沒有條件也沒有休息,因此它將永遠運行。 – Adrian

+0

@ scott-stensland,編輯不正確。由於我有無限的while循環,所以代碼永遠不會達到「done < - true return」。我希望能打破循環,因爲我需要保持調用url直到沒有更多記錄返回。 – lmln

+0

@adrian,我希望打破循環,如果更多{},因爲我需要保持調用的網址,直到沒有更多的記錄返回。 – lmln

回答

0

這是我一直在尋找。我有一個無限循環中的數字生成器。程序在某些情況下退出,在這個例子中,它在j值上,但它也可以是其他值。

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\n", j) 
     } else { 
      done = true 
     } 
    } 
} 
2

根據定義,一個無條件的for循環是一個無限循環。除非你用一些邏輯來打破這個無限循環,否則你永遠不會擺脫它。

在您的遊樂場您的評論意味着您想發送3個工作。您應相應地改變你的for循環:

for j := 0; j < 3; j++ { 
    jobs <- j 
    fmt.Println("sent job", j) 
} 
1

這是工人的簡化版本。它的不是很對生產水平的流量是有用的,但應該作爲一個簡單的例子,有噸他們:-)

package main 

import (
    "log" 
    "sync" 
) 

type worker struct { 
    jobs chan int 
    wg *sync.WaitGroup 
} 

func main() { 
    w := worker{ 
     jobs: make(chan int, 5), // I only want to work on 5 jobs at any given time 
     wg: new(sync.WaitGroup), 
    } 

    for i := 0; i < 3; i++ { 
     w.wg.Add(1) 
     go func(i int) { 
      defer w.wg.Done() 
      w.jobs <- i 
     }(i) 

    } 

    // wait in the background so that i can move to line 34 and start consuming my job queue 
    go func() { 
     w.wg.Wait() 
     close(w.jobs) 
    }() 

    for job := range w.jobs { 
     log.Println("Got job, I should do something with it", job) 
    } 

}