-3

As every new programer starting in Go 1st think you see is, strict code format. Means:

//Valid
func foo(){
}

//Invalid
func foo()
{
}

Same goes for if-else that else should be in same line where if ends, like:

//Valid
if{
}else{
}

//Invalid
if{
}
else{
}

we get below error:

syntax error: unexpected else, expecting }

I have checked the spec spec, but not able to find why.

The only explanation I'am getting is its mandatory.

Can anyone explain us why this is mandated does this have any reason? if any.

UPDATE
I think i have mention this already that "I know lang say so", Question is WHY? Why to go this length to make it compile time error, what problems it was posing if we don't do this?

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89
  • 4
    This is clearly explained here: https://golang.org/doc/effective_go.html#semicolons – slebetman Jan 21 '20 at 11:14
  • If a line ends with `}`, a semicolon is automatically inserted which "renders" your code invalid. [Spec: Semicolons](https://golang.org/ref/spec#Semicolons). – icza Jan 21 '20 at 11:16
  • 2
    @usr The dup contains the exact quote from the spec that says why it's invalid. – icza Jan 21 '20 at 11:33
  • I think i have mention this already that i know lang say so, Question is WHY? Why it has to go this length to make it compile time error. – Sumit Singh Jan 21 '20 at 11:35
  • 1
    If the question is "why was Go designed this way", that's a question for the designers, not the community. – Adrian Jan 21 '20 at 15:24
  • Because Rob Pike and Ken Thompson like it that way. – torek Jan 21 '20 at 19:52
  • Does this answer your question? [How to break a long line of code in Golang?](https://stackoverflow.com/questions/34846848/how-to-break-a-long-line-of-code-in-golang) – Chetan Kumar Jan 22 '20 at 09:09

2 Answers2

2

The language was designed as such and the reason is outlined in the FAQ.

https://golang.org/doc/faq

Why are there braces but no semicolons? And why can't I put the opening brace on the next line?

o uses brace brackets for statement grouping, a syntax familiar to programmers who have worked with any language in the C family. Semicolons, however, are for parsers, not for people, and we wanted to eliminate them as much as possible. To achieve this goal, Go borrows a trick from BCPL: the semicolons that separate statements are in the formal grammar but are injected automatically, without lookahead, by the lexer at the end of any line that could be the end of a statement. This works very well in practice but has the effect that it forces a brace style. For instance, the opening brace of a function cannot appear on a line by itself.

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. That style may differ from what you've used in C or Java, but Go is a different language and gofmt's style is as good as any other. More important—much more important—the advantages of a single, programmatically mandated format for all Go programs greatly outweigh any perceived disadvantages of the particular style. Note too that Go's style means that an interactive implementation of Go can use the standard syntax one line at a time without special rules.

P.P
  • 117,907
  • 20
  • 175
  • 238
1

Go inserts a ; at the end of lines ending in certain tokens including }. Since if {...} else {...} is a single statement, you can't put a semicolon in the middle of it after the first closing brances i.e. }, hence the requirement to put } else { on one line is mandatory.

I hope it answers your question.

Chetan Kumar
  • 1,331
  • 1
  • 10
  • 16