12

I have a struct:

type nameSorter struct {
    names []Name
    by    func(s1, s2 *Name) bool

Which is used in this method. What is going on with that comma? If I remove it there is a syntax error.

func (by By) Sort(names []Name) {
        sorter := &nameSorter{
            names: names,
            by:    by, //why does there have to be a comma here?
        }
        sort.Sort(sorter)

Also, the code below works perfectly fine and seems to be more clear.

func (by By) Sort(names []Name) {
    sorter := &nameSorter{names, by}
    sort.Sort(sorter)

For more context this code is part of a series of declarations for sorting of a custom type that looks like this:

By(lastNameSort).Sort(Names)
John
  • 121
  • 1
  • 6
  • 2
    See this possible duplicate: [How to break a long line of code in Golang?](http://stackoverflow.com/questions/34846848/how-to-break-a-long-line-of-code-in-golang/34848928#34848928) – icza Apr 07 '17 at 09:46
  • So the comma needs to be there because of the line break before the " } " . Thanks for the link. – John Apr 07 '17 at 10:07
  • It's basically a side-effect of allowing you (in fact, encouraging you) to omit semicolons. IIRC, the error used to be unexpected semicolon, because the compiler adds a semicolon at the end of the line at compile time, which would then be invalid syntax. – Adrian Apr 07 '17 at 19:35

2 Answers2

17

This is how go works, and go is strict with things like comma and parentheses.

The good thing about this notion is that when adding or deleting a line, it does not affect other line. Suppose the last comma can be omitted, if you want to add a field after it, you have to add the comma back.

See this post: https://dave.cheney.net/2014/10/04/that-trailing-comma.

cizixs
  • 12,931
  • 6
  • 48
  • 60
  • 7
    Another reason they did it was for code reviews: when adding another line, you don't have to modify the previous line to add a comma - which would create 2 lines modified in a CR/PR, etc. By enforcing that comma, it cuts down on noise. This was actually a coding guideline I enforced with C# as it was optional. – eduncan911 Apr 07 '17 at 11:51
  • Trailing commas are also _allowed_ in most places in C, Python, Ruby, Java and many other languages, and many style guides encourage them. Go is unusual in _requiring_ them, but note the requirement only applies when closing brace/bracket/paren is on separate line. – Beni Cherniavsky-Paskin Jul 15 '20 at 07:40
5

From https://golang.org/doc/effective_go.html#semicolons:

the lexer uses a simple rule to insert semicolons automatically as it scans, so the input text is mostly free of them

In other words, the programmer is unburdened from using semicolons, but Go still uses them under the hood, prior to compilation.

Semicolons are inserted after the:

last token before a newline is an identifier (which includes words like int and float64), a basic literal such as a number or string constant, or one of the tokens break continue fallthrough return ++ -- ) }

Thus, without a comma, the lexer would insert a semicolon and cause a syntax error:

   &nameSorter{
       names: names,
       by:    by; // semicolon inserted after identifier, syntax error due to unclosed braces
   }
jordanpg
  • 6,386
  • 4
  • 46
  • 70