Created
September 4, 2019 21:53
-
-
Save kaipyroami/d5263784ca3344ebf80cb2f8b6119be3 to your computer and use it in GitHub Desktop.
An example of a channel being piped into a range loop in Go.
This file contains 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() { | |
c := make(chan int) | |
go func() { | |
for i := 0; i < 5; i++ { | |
c <- i | |
fmt.Println("func loop:", i) | |
} | |
close(c) | |
}() | |
for v := range c { | |
fmt.Println(v) | |
} | |
fmt.Println("about to exit") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
func loop: 0
0
1
func loop: 1
func loop: 2
2
3
func loop: 3
func loop: 4
4
about to exit