-4

why is there a comma in this variable declaration:

// RinkebyBootnodes are the enode URLs of the P2P bootstrap nodes running on the
// Rinkeby test network.
var RinkebyBootnodes = []string{
    "enode://a24ac7c5484ef4ed0c5eb2d36620ba4e4aa13b8c84684e1b4aab0cebea2ae45cb4d375b77eab56516d34bfbd3c1a833fc51296ff084b770b94fb9028c4d25ccf@52.169.42.101:30303", // IE
    "enode://343149e4feefa15d882d9fe4ac7d88f885bd05ebb735e547f12e12080a9fa07c8014ca6fd7f373123488102fe5e34111f8509cf0b7de3f5b44339c9f25e87cb8@52.3.158.184:30303",  // INFURA
}

I am talking about comma that goes here:

30303",  // INFURA

it is the last character of the string array, and it has to go there, otherwise I get a compile error.

In C language you can't have commas at the end of curly braces {} , but in go you have to. Why ? And what does this comma mean?

Nulik
  • 6,748
  • 10
  • 60
  • 129
  • 3
    For example: https://dave.cheney.net/2014/10/04/that-trailing-comma – Sergio Tulentsev Oct 17 '17 at 12:12
  • See related questions: [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/34848928#34848928); and [MongoDB in Go with mgo, operators with bson.M / bson.D always got syntax error](https://stackoverflow.com/questions/42554759/mongodb-in-go-with-mgo-operators-with-bson-m-bson-d-always-got-syntax-error/42554846#42554846). – icza Oct 17 '17 at 12:14
  • 1
    That's mandated by the language spec. – Volker Oct 17 '17 at 12:18
  • 1
    @icza: It has nothing to do with the semicolon rules. It predates them. As already noted ([That trailing comma](https://dave.cheney.net/2014/10/04/that-trailing-comma)) it helps with maintaining and generating lists of elements. It is by design. – peterSO Oct 17 '17 at 12:24
  • 1
    @peterSO But quoting from that blog you linked too, even Dave Cheney thinks [_"it is the result of the semicolon rule"_](https://dave.cheney.net/2014/10/04/that-trailing-comma). Reading forward: _"Although possibly an [unintended consequence](http://golang.org/doc/faq#semicolons), this means that when proposing a one line change, it really is a one line change."_ – icza Oct 17 '17 at 12:31
  • I am surprised how many people downvoted this question. My surprise is valid, this comma only shows laziness of Go develperers who designed the grammar. – Nulik Oct 17 '17 at 18:32

1 Answers1

1

Comma is an element splitter. Elements could be.written as in a row so in a column:

{1, 2, 3}


{
 1,
 2,
 3,
 }

It’s evident comma is unnecessary after the last element. Some languages require their absense like C, some - presence like Go, some allow both variants like Python. Sometimes it causes bugs like with JavaScript in old Internet Explorer.

Why do gophers decide to keep this comma? One of key principles of Go is visual simplicity and readability. This way all the strings look the same way. It’s easier to read them by eyes. Also suppose you add one more line - you have to edit previous one. And this creates visual noise in diff.

Thinking the same you get to the idea comma should not be in one liners. Because spoils readability as extra symbol.

Eugene Lisitsky
  • 12,113
  • 5
  • 38
  • 59