Use make(chan T) to create a channel, then spawn goroutines to send data and a separate goroutine to receive it.
package main
import "fmt"
func main() {
ch := make(chan string)
go func() {
ch <- "Hello from goroutine"
}()
msg := <-ch
fmt.Println(msg)
}
- Create an unbuffered channel with
ch := make(chan string). - Start a goroutine to send a value using
ch <- "value". - Receive the value in the main function using
msg := <-ch. - Print the received message to verify the Pub/Sub flow.