liveBook · Manning

Goroutine

As explained in Build and Orchestrator in Go (From Scratch)

A goroutine is a lightweight concurrent execution unit managed by the Go runtime.

A goroutine starts a function concurrently with the rest of a Go program. Prefix a call with `go`, such as `go runTasks(&w)`, so `main` can continue starting the API while `runTasks` repeatedly checks the worker queue and runs pending tasks. section 5.6

The practical model is separation of responsibilities: request handlers accept requests and enqueue work, while a background goroutine performs task operations. This keeps the API from directly starting or stopping tasks and lets it remain available while work is processed. section 5.6

Use separate goroutines when independent worker activities must proceed together:

  • Run a continuous queue-processing loop that invokes `RunTask` when work is available. section 5.6
  • Periodically collect machine statistics with `w.CollectStats()`. section 7.5
  • Start the API server concurrently rather than occupying the main goroutine. section 7.5
  • Run an anonymous loop that periodically updates task information from workers while another loop reports task state. section 7.5

Worker components running together

A worker can launch queue processing, statistics collection, and API serving with three `go` calls. Each component then proceeds concurrently: the task loop sleeps between polls, stats collection runs periodically, and the API listens for requests. section 7.5 section 5.6

Listing 7.11 Running all the worker’s components in separate goroutines
func main() {
   host := os.Getenv("CUBE_HOST")
   port, _ := strconv.Atoi(os.Getenv("CUBE_PORT"))

   fmt.Println("Starting Cube worker")

   w := worker.Worker{
       Queue: *queue.New(),
       Db:    make(map[uuid.UUID]*task.Task),
   }
   api := worker.Api{Address: host, Port: port, Worker: &w}

   go runTasks(&w)
   go w.CollectStats()
   go api.Start()

This listing places each worker responsibility behind an explicit `go` call, making the concurrent structure visible before manager setup and task submission. section 7.5

This approach is deliberately operational rather than a full treatment of goroutine internals. The important decision is which activities need to continue independently: task execution, machine-stat collection, API serving, and manager updates. section 5.6 section 7.5

How Build and Orchestrator in Go (From Scratch) covers it

How deep it goes
Demonstrates `go` calls, continuous loops, and anonymous functions, while deferring goroutine implementation details.
Examples
Uses a queue-polling task loop, concurrent statistics collection and API serving, and a periodic manager-update loop.
What it assumes
Assumes familiarity with Go function calls and introduces the `go` prefix through worker startup code.
What you can do after
Apply it to run worker task processing, statistics collection, and API serving concurrently.
How it connects
Connects goroutines to APIs, workers, managers, queues, task processing, and machine-stat collection.

Also explained in

Compare how 7 books explain goroutine →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build and Orchestrator in Go (From Scratch). The section links point to the parts of the book it draws on.