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.

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

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来