Created
March 7, 2022 00:14
-
-
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
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" | |
) | |
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