duanchi4544 2017-10-17 12:10
浏览 261
已采纳

数组定义中的逗号是什么意思?

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?

  • 写回答

1条回答 默认 最新

  • duanpingzu7194 2017-10-17 18:15
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低