Channel Directions
package main
import "fmt"
// Function ping sends a message to the provided channel
func ping(pings chan<- string, msg string) {
pings <- msg
}
// Function pong receives a message from one channel and sends it to another channel
func pong(pings <-chan string, pongs chan<- string) {
msg := <-pings
pongs <- msg
}
func main() {
// Creating two buffered channels, 'pings' and 'pongs', each with a capacity of 1
pings := make(chan string, 1)
pongs := make(chan string, 1)
// Sending a message to the 'pings' channel
ping(pings, "passed message")
// Executing the 'pong' function with the 'pings' and 'pongs' channels
pong(pings, pongs)
// Receiving and printing the final message from the 'pongs' channel
fmt.Println(<-pongs)
}Output
Last updated