Thursday, June 24, 2010

Curly Brace { } Mayhem

Programmers and enthsiasts new to Go may find the curly brace syntax requirements to be a bit unusual. Misplacing the opening curly brace in functions and other programming structures is a very common mistake.

Let's examine this a little closer:

func main()
{

}

will generate an error, because for programming elements like functions, Go inserts a semicolon after the closing parenthesis. Thus, this is what the compiler actually sees:

func main();
{

}

which is obviously a syntax error.

To prevent this, make sure that you put the opening curly brace on the same line as the function header, as follows:

func main(){

}

With the above syntax template, the program will compile with no problem!

No comments:

Post a Comment