LESSON · 1 OF 19

What is a goroutine

What is a goroutine

A goroutine is a function running concurrently with the rest of your program. Starting one is the smallest bit of syntax in Go: put the word go in front of a function call.

go
go doWork()

That call returns immediately. doWork keeps running on its own while the line after go doWork() executes right away. You now have two things happening at once: the goroutine, and whatever the calling code does next.

You can start a goroutine from any function, and the function you launch can take arguments like any normal call:

go
go handle(req)

The one thing to hold onto: go does not wait. It fires the function off and moves on. Everything else in this topic is about dealing with that fact.

Spin up a fresh environment and practice live.
go · fresh machine · ready in under a minute