goroutines done channel unbuffered channel

In unbuffered channel by default sends and receives block until both the sender and receiver are ready. package main import ( "fmt" "time" ) func worker(done chan bool) { fmt.Print("working…") fmt.Println("done") done <- true } func main() { done := make(chan bool) go worker(done) time.Sleep(5 * time.Second) <-done } But the above code is executed… Read More goroutines done channel unbuffered channel

Incorrect output by goroutines

I am watching a lecture in which author build a status app using go routines which is behaving weirdly after sometime. Here’s the code: func main() { links := []string{ "http://google.com&quot;, "http://facebook.com&quot;, "http://stackoverflow.com&quot;, "http://amazon.com&quot;, "http://golang.org&quot;, } c := make(chan string) for _, link := range links { go checkLink(link, c) } for l := range… Read More Incorrect output by goroutines

Do I have to wrap a function with goroutine to use timeout?

So, basically, is there any difference between these 2 blocks of code? res := make(chan error, 1) go func() { res <- slowOperation() }() select { case <-time.After(deadline): return errors.New("deadline exceeded") case err := <-res: return err } and res := make(chan error, 1) select { case <-time.After(deadline): return errors.New("deadline exceeded") case res <- slowOperation():… Read More Do I have to wrap a function with goroutine to use timeout?

How to check for goroutine completion while actively reading from channel?

Had a hard time expressing this question in a sentence. Here is the situation: I’m trying to spawn off a set of goroutines to recurse over directories and find matching files. Then I collect those files and continue on to process them. However, the catch is that I don’t know how many files each routine… Read More How to check for goroutine completion while actively reading from channel?

How to make Go channel worker have different result's length?

I made some edits from the gobyexample: import ( "fmt" "math/rand" "time" ) type DemoResult struct { Name string Rate int } func random(min, max int) int { rand.Seed(time.Now().UTC().UnixNano()) return rand.Intn(max-min) + min } func worker(id int, jobs <-chan int, results chan<- DemoResult) { for j := range jobs { fmt.Println("worker", id, "started job", j)… Read More How to make Go channel worker have different result's length?