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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?