Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Keywords and Statements

The go statement

Reference for the Go go statement: start a function call in a new goroutine, when arguments are evaluated, waiting with WaitGroup, loop variables and leaks.

The go statement starts the execution of a function call as an independent goroutine, a lightweight thread managed by the Go runtime. The statement itself returns immediately; the new goroutine runs concurrently with the code after it.

Like defer, the function value and the arguments are evaluated in the calling goroutine at the moment of the go statement. The result values of the call are discarded. When main returns, the program ends, even if other goroutines are still running.

Go
go worker(jobs, results)
go func() {
    fmt.Println("running concurrently")
}()

Try it

Go Wait for goroutines with a WaitGroup

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Arguments are evaluated at the go statement

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
GoStmt = "go" Expression .

The expression must be a function or method call, with the same restrictions on built-in functions as for statement calls.

Part Description
Function value Evaluated in the current goroutine when the statement executes.
Arguments Evaluated in the current goroutine and passed to the new one.
Return values Discarded. Use a channel, a shared variable guarded by a mutex, or an errgroup-style helper to get results out.

Usage notes

  • Main exits, program exits. Other goroutines are not waited for. Use a sync.WaitGroup, a channel or context to wait for them.
  • No identity or handle. A goroutine cannot be killed from outside. Design them to stop by listening on a context.Context or a done channel.
  • Loop variables. Since Go 1.22, each iteration of a for loop has its own variable, so go func() { use(i) }() sees the right value. In earlier versions you had to pass i as an argument.
  • Data races. Two goroutines accessing the same variable, with at least one write and no synchronization, is a data race. Use channels, sync, or sync/atomic, and test with go test -race.
  • Leaks. A goroutine blocked forever on a channel or lock is never reclaimed. Always give goroutines a way to finish.
  • Panics. An unrecovered panic in any goroutine crashes the whole program. Recover inside the goroutine if you need to contain it.
  • Cost. A goroutine starts with a small stack (a few kilobytes) that grows as needed, so thousands or millions are practical. They are multiplexed onto OS threads by the scheduler; GOMAXPROCS defaults to the number of CPUs.
  • Method values. go obj.Method() evaluates obj (and copies it for value receivers) at the go statement.