Created
February 13, 2020 17:44
-
-
Save piEsposito/44afe1165c90f87e2f6ac75056e0c866 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" | |
| "time" | |
| ) | |
| func feedChannel(c chan int) { | |
| //aqui vamos feedar nosso canal | |
| for i := 0; i < 10; i++ { | |
| c <- i | |
| } | |
| } | |
| func printFromChannel(c chan int) { | |
| //nessa função, simplesmente printamos o que está no canal | |
| for len(c) > 0 { | |
| fmt.Println(<-c) | |
| } | |
| } | |
| func main() { | |
| c := make(chan int, 10) //nosso canal tem tamanho 10 | |
| //feedamos o canal de forma concorrente | |
| for i := 0; i < 3; i++ { | |
| go feedChannel(c) | |
| } | |
| time.Sleep(5 * time.Second) | |
| //printamos do canal de forma concorrente | |
| for i := 0; i < 3; i++ { | |
| go printFromChannel(c) | |
| } | |
| time.Sleep(5 * time.Second) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment