Created
February 13, 2020 17:57
-
-
Save piEsposito/dba36253f370ae8c1421ffc4149df5c5 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 feedChannel(c chan int, wg *sync.WaitGroup) { | |
//aqui vamos feedar nosso canal | |
for i := 0; i < 10; i++ { | |
c <- i | |
} | |
wg.Done() | |
} | |
func printFromChannel(c chan int, wg *sync.WaitGroup) { | |
//nessa função, simplesmente printamos o que está no canal | |
for len(c) > 0 { | |
fmt.Println(<-c) | |
} | |
wg.Done() | |
} | |
func main() { | |
c := make(chan int, 100) //nosso canal tem tamanho 10 | |
var wg sync.WaitGroup // nosso wait_group | |
//feedamos o canal de forma concorrente | |
for i := 0; i < 3; i++ { | |
wg.Add(1) | |
go feedChannel(c, &wg) | |
} | |
wg.Wait() | |
//printamos do canal de forma concorrente | |
for i := 0; i < 3; i++ { | |
wg.Add(1) | |
go printFromChannel(c, &wg) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment