Skip to content

Instantly share code, notes, and snippets.

@kaipyroami
Created September 4, 2019 21:53
Show Gist options
  • Save kaipyroami/d5263784ca3344ebf80cb2f8b6119be3 to your computer and use it in GitHub Desktop.
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.
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")
}
@kaipyroami
Copy link
Author

Output:

func loop: 0
0
1
func loop: 1
func loop: 2
2
3
func loop: 3
func loop: 4
4
about to exit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment