Created
March 22, 2020 12:00
-
-
Save dumindu/d9083161bf1fcb2bd38d7f0227aae180 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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