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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵