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 worker(jobs, results) go func() { fmt.Println("running concurrently") }()
Try it
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.
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
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 orcontextto wait for them. - No identity or handle. A goroutine cannot be killed from outside. Design them to stop by listening on a
context.Contextor a done channel. - Loop variables. Since Go 1.22, each iteration of a
forloop has its own variable, sogo func() { use(i) }()sees the right value. In earlier versions you had to passias 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, orsync/atomic, and test withgo 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;
GOMAXPROCSdefaults to the number of CPUs. - Method values.
go obj.Method()evaluatesobj(and copies it for value receivers) at thegostatement.
Related
chan,select,defer,sync.WaitGroup,sync.Mutex- Lessons: Goroutines, Channels, Data races