Skip to content

Instantly share code, notes, and snippets.

@armando-couto
Created March 7, 2022 00:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save armando-couto/f27993f4154e069f4b5d3aa83acd3fbf to your computer and use it in GitHub Desktop.
Save armando-couto/f27993f4154e069f4b5d3aa83acd3fbf to your computer and use it in GitHub Desktop.
For now, let’s look at an example to help clarify these concepts. Let’s create a gorou‐ tine that clearly owns a channel, and a consumer that clearly handles blocking and closing of a channel
package main
import (
"fmt"
)
func main() {
chanOwner := func() <-chan int {
resultStream := make(chan int, 5)
go func() {
defer close(resultStream)
for i := 0; i <= 5; i++ {
resultStream <- i
}
}()
return resultStream
}
resultStream := chanOwner()
for result := range resultStream {
fmt.Printf("Received: %d\n", result)
}
fmt.Println("Done receiving!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment