Go statements

July 9, 2024

At long last, we’re ready to learn about Go’s namesake feature!

Go statements

A “go” statement starts the execution of a function call as an independent concurrent thread of control, or goroutine, within the same address space.

GoStmt = "go" Expression .

Now before we talk about the particulars of how a go statement works, I want to set aside a common misconception, that’s subtly addressed in the above description.

Many newcomers to Go, and in fact, many seasoned Go developers, often conflate the concepts of “function” and “goroutine”, using the terms somewhat interchangeably. Some will talk about a piece of code as “a goroutine”, or with phrases such as “this function is a goroutine.”

However, these concepts are not the same!

While it may seem like a matter of trivia, or nit-picking, it can lead to some seroius confusion in certain circumstances.

Functions are collections of code. Goroutines are “independent threads of control”.

I think it can help clear up the confusion to point out that there’s not a one-to-one relationship between goroutines and functoins.

A single function can be executed by any number of goroutines, so clearly a function cannot itself be a goroutine.

Further, a typical goroutine executes many functions–it has a single function as its entry point, but then any functions that function calls (directly or indirectly), are also executed in the same goroutine. At least until another go statement is encountered, which launches another goroutine with a new function entry point.

Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)


Share this

Direct to your inbox, daily. I respect your privacy .

Unsure? Browse the archive .

Related Content


Go statements, conclusion

Today we finish the description of go statements: Go statements … When the function terminates, its goroutine also terminates. If the function has any return values, they are discarded when the function completes. go Server() go func(ch chan<- bool) { for { sleep(10); ch <- true }} (c) Okay, so that bit about discarding return values makes sense, right? func main() { go sum(1, 3) // return value discarded } func sum(a, b int) int { return a + b } But what if you need that return value for something?


Go statements, continued

Now that we’ve talked about what goroutines are and aren’t, lets look at the details of a Go statement: Go statements … The expression must be a function or method call; it cannot be parenthesized. This is important! The expression must be a functino or method call. Not simply the name of a function or method. That is to say, this: go foo() not that: go foo This may feel pretty natural, but it’s easy (at leat for me) to forget sometimes.


Built-in functions

We’ve just finished up the rather long section on different types of statements. There’s only a few more sections in the spec, before we finish this series. And today we start looking at Built-in functions! Built-in functions Built-in functions are predeclared. They are called like any other function but some of them accept a type instead of an expression as the first argument. The built-in functions do not have standard Go types, so they can only appear in call expressions; they cannot be used as function values.

Get daily content like this in your inbox!

Subscribe