douzhang8840 2015-12-31 13:57
浏览 16
已采纳

这个数组初始化语法是什么意思? (带键的元素)

I recently found the code below:

var noEscape = [256]bool{
    'A': true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
    'a': true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true,
    '0': true, true, true, true, true, true, true, true, true, true,
    '-': true,
    '.': true,
    '_': true,
    '~': true,
}

I understand that this [N]bool is initialized with N false (zeros) by default. And if the index followed by a colon is specified values can are defined form the index (inclusive). Where is it described? What types can be used as an index value (there is a char in the example)?

  • 写回答

1条回答 默认 最新

  • dongqiang5541 2015-12-31 14:24
    关注

    For an array or slice literal, the index values must be constants. From the docs on "composite literals"

    For array and slice literals the following rules apply:

    • Each element has an associated integer index marking its position in the array.
    • An element with a key uses the key as its index; the key must be a constant integer expression.
    • An element without a key uses the previous element's index plus one. If the first element has no key, its index is zero.

    The literal characters in the example are untyped constants, which just happen to be written as a single rune literal. If you were to assign any of those values to a variable to use as the index, the code would not compile.

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

报告相同问题?

悬赏问题

  • ¥15 前后端分离的学习疑问?
  • ¥15 stata实证代码答疑
  • ¥15 MATLAB数据处理插值
  • ¥50 husky+jaco2实现在gazebo与rviz中联合仿真
  • ¥15 dpabi预处理报错:Error using y_ExtractROISignal (line 251)
  • ¥15 在虚拟机中配置flume,无法将slave1节点的文件采集到master节点中
  • ¥15 husky+kinova jaco2 仿真
  • ¥15 zigbee终端设备入网失败
  • ¥15 金融监管系统怎么对7+4机构进行监管的
  • ¥15 硬件IIC从模式的数据发送,中断数据的接收,不能用HAL库(按照时序图)