Missing function body with "{" on second line

I copied and pasted the hello.go source into an editor.
By accident I pressed the return key. This placed
the “{” on the line below the “func main()” line.
When I ran the “go build hello.go” command I
received an error message “missing function body”.

I only realized that I had made this change, and fixed it.

However, I like to place the “{” and “}” characters on separate
lines (column aligned), So I can see if I am missing one of the
characters. Is the placing of the “{” a requirement of the go
language? Is there an “escape” character that allows the line
to continue onto the next line?

Also, in the past I have found that I tend to miss placing a
comma at the end of a line with the next arguement on a
separate line. So I like to start the next line with the comma,
rather than ending the previous line with a comma. In this
way I can add another entry to an argument even at the end of the list without having to go up and add the missing comma. The only
exception is adding a new first entry. And I can easily see if I
have missed a comma, since it is at the beginning of the line.
Is this going to be a problem as well?
go

For the sake of speed and/or simplicity, Go’s parser requires trailing commas and trailing curly braces. Because the parser is implemented this way, there’s now only one brace style whether you like it or not. I was really angry about it when I started programming in Go; in my head it’s jarring to see that trailing comma in:

x := []int{
  1,
  2,
  3,
}

However, after a few years, I’m over it. The gofmt tool will also reformat your code to make it right. Rob Pike once said that nobody likes exactly how gofmt formats code; not even the guy who wrote it (I couldn’t find his name in the top few results after a Google search). The point is that it’s consistent.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.