Newbie syntax mystery

I am a new newbie! I installed Go and tried the Hello World program:

package main

import "fmt"

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

I’m an old C programmer and like a slightly different layout style:

package main

import "fmt"

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

but it gives a syntax error. I looked at the Go syntax/grammar page and it only mentions newline under whitespace. This is a mystery to me.

Hi, @bobkummerfeld, Go is very strict about that aspect of its syntax and does not allow curly braces to be put on separate lines. It makes parsing easier and eliminates at least one of the questions in the brace/newline/tabs style wars!

EDIT: Relevant link to the FAQ: http://golang.org/doc/faq#semicolons

2 Likes

@bobkummerfeld as a C programmer u would have used vim as ur editor more which is very good. However, here u can use the IDE Goland - it will handle these things.

Thanks for the help. A standard style is probably a good thing! I’m surprised there was no comment about it in the formal docs.

There is: https://golang.org/doc/effective_go.html. Unlike C (was a kernel developer), Go explicitly selected certain rules as its default we have to bear with it. One notable rule is the selected CamelCase over others.

The actual VIM way is to use golang-lint (https://github.com/golangci/golangci-lint) and vim-go (https://github.com/fatih/vim-go).

3 Likes

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