Create a file named app.go
and paste following code in it:
package main
import (
"fmt"
"strconv"
"time"
)
func main() {
work := make(chan string)
go producer(work)
go consumer(work)
fmt.Scanln()
}
func producer(work chan string) {
index := 1
for {
work <- "work " + strconv.Itoa(index)
time.Sleep(time.Millisecond * 500)
index++
}
}
func consumer(work chan string) {
for w := range work {
fmt.Println(w)
}
}
And then run it:
go run app.go