Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dumindu/d9083161bf1fcb2bd38d7f0227aae180 to your computer and use it in GitHub Desktop.
Save dumindu/d9083161bf1fcb2bd38d7f0227aae180 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"sync"
)
func main() {
ch := make(chan int)
go write(ch)
for v := range ch {
fmt.Println(v)
}
}
func write(ch chan int) {
wg := sync.WaitGroup{}
wg.Add(10)
for i := 1; i <= 10; i++ {
go func(v int) {
ch <- v // 💡 Unexpected results can be got, if we send i directly
wg.Done()
}(i)
}
wg.Wait()
close(ch)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment