One-to-many messaging with chan in Go
The chan type in Go is a mechanism which can be used to send or receive data from one function to another. E.g:
func main() {
c := make(chan string)
go printHello(c)
sayHello(c)
}
func sayHello(msgChan chan<- string) {
msgChan <- "hello"
}
func printHello(msgChan <-chan string) {
fmt.Println(<-msgChan)
}Channels can be bidirectional (chan string) or directional (chan <- string sender/<-chan string receiver). And they operate like FIFO queues.