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.

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

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改