Tuesday, October 01, 2019

Gotcha: go Foo().Bar()

To my surprise, but in accordance with the Go language specification, the following code invokes Foo() in the current goroutine and then starts a separate goroutine to invoke Bar():

    go Foo().Bar()


I incorrectly assumed that it created a separate goroutine to evaluate the entire expression.  I'm not sure why I assumed that.  If you want both Foo and Bar to be run inside a new goroutine, you need something like this:

    go func () { Foo().Bar() }()


Hopefully writing this down will help me remember.