Go statements, continued

July 11, 2024

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. Especially when using an anonymous function. I very frequently make the mistake of typing this:

go func() {
	/* do a thing */
}

which is invalid. I need to add the () at the end to make it a function call:

go func() {
	/* do a thing */
}()

… Calls of built-in functions are restricted as for expression statements.

If you need to use a built-in function, then, you’ll need to wrap it in another (likely anonymous) function:

go func() {
	close(myChannel)
}()

(Although honestly, I’m not aware of any reason to ever call a builtin function by iteslf in a new goroutine).

The function value and parameters are evaluated as usual in the calling goroutine, but unlike with a regular call, program execution does not wait for the invoked function to complete. Instead, the function begins executing independently in a new goroutine.

This is, of course, what makes go statements special. The functions called with such a statement run independently of the current program execution flow. This is the primary Go way to write asynchronous code.

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

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.


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