...
In the above code, the program exits once the count routine returns a message on the channel.
Code Block |
---|
package main
import (
"fmt"
"time"
)
func main(){
//make channel of type string
c := make(chan,string)
//run in background
go count("sheep", c)
//loop until count function closes the channel
for {
msg, open := <- c
if !open {
//channel closed, break out of for loop
break
}
}
fmt.Println(msg)
}
func count(thing string, c chan string) {
for i:=0; i <= 5; i++ {
//send message on channel
c <- thing
time.Sleep(time.Millisecond * 500)
}
//close channel
close(c)
} |
References
Reference | URL |
---|---|
Learn Go in 12 Minutes | https://www.youtube.com/watch?v=C8LgvuEBraI |
Go Packages | https://golang.org/pkg/ |
Go By Examples | https://gobyexample.com/ |
...