Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A simple Goroutine exercise, where a worker thread reads user input through a channel
package main
import (
type message struct {
id int
content string
func (m *message) getID() string {
return fmt.Sprintf("#%d",
func readerWorker(readQueue <-chan message) {
for {
queueMessage := <-readQueue
time.Sleep(3000 * time.Millisecond)
fmt.Printf("Worker reads message %s from the queue: %s", queueMessage.getID(), queueMessage.content)
func main() {
id := 1000
reader := bufio.NewReader(os.Stdin)
readQueue := make(chan message, 3)
go readerWorker(readQueue)
for {
fmt.Print("Enter text: ")
userInput, _ := reader.ReadString('\n')
if userInput == "exit\n" {
queueMessage := message{id: id, content: userInput}
readQueue <- queueMessage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.