Missing function body

yourbasic.org/golang

As you may have noticed, programs with an opening brace on separate line do not compile.

func main()
{
	fmt.Println("Hello")
}
../main.go:1:6: missing function body for "main"
../main.go:2:1: syntax error: unexpected semicolon or newline before {

You must write:

func main() {
	fmt.Println("Hello")
}

This is a trade-off in the design of the Go language.

Some have argued that the lexer should do lookahead to permit the brace to live on the next line. We disagree. Since Go code is meant to be formatted automatically by gofmt, some style must be chosen. […] The advantages of a single, programmatically mandated format for all Go programs greatly outweigh any perceived disadvantages of the particular style. Go FAQ: Why are there braces but no semicolons? And why can’t I put the opening brace on the next line?

Further reading

Learn to love your compiler is a list of common Go compiler error messages: what they mean and how to fix the problem.

Share this page: