Skip to content
December 5, 2010 / Rohit

Go and Curly Braces

I decided to give Google’s Go Programming language a shot. The installation is not the smoothest — you need to pull the source using Mercurial; download and install dependencies; and then build from source. But anyway, as with any other language it was the hello world example which I wrote first:

package main
import fmt "fmt"

func main()
    fmt.Printf("Hello, world\n");

But on giving the command

6g hello.go

(which is a weird name for a compiler), I got an error:

hello.go:5: syntax error: unexpected semicolon or newline before {

Which seemed weird, because as far as I was concerned the code was an accurate representation of the example code. The error was on Line 5, the one which had the curly brace after the “main” function. How could it be wrong?

Turns out in Go, you have to put the brace on the same line as the function name. So instead of:

func main()

You need to write:

func main() {

The same style is followed for all statements which might need curly braces — conditionals, loops, etc. The reason for this is to make the compiler faster.

I personally feel that the choice of where to put the curly braces should be left to the programmer and not enforced by the compiler. If not for anything else, it will make programmers — coming from C, C++, C#, Java — try out Go without correcting their brace-location constantly.

As Christophe de Dinechin points out, there is already a thread on the Go language group which has over a 100 posts discussing this.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: