Skip to content

Instantly share code, notes, and snippets.

@piEsposito
Created February 13, 2020 17:44
Show Gist options
  • Select an option

  • Save piEsposito/44afe1165c90f87e2f6ac75056e0c866 to your computer and use it in GitHub Desktop.

Select an option

Save piEsposito/44afe1165c90f87e2f6ac75056e0c866 to your computer and use it in GitHub Desktop.
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