douyuan1049 2015-03-31 09:26
浏览 20

Golang常数2D数组语法失败[重复]

This question already has an answer here:

I want to declare a constant golang 2d array (not slices), but I can't figure it out, having looked at the other golang comments on this issue.

type fooT [1][1]float64
const BAR fooT = {[1]float64 {.01}}

Gives the error fubar.go:5: syntax error: unexpected {. But the following compiles fine:

type fooT [1][1]float64
var BAR = fooT {[1]float64 {.01}}

First, I do not understand why I need to redeclare the underlying array redundantly, and it does seem golang compiler knows the type because it gives an error if I change it. But, why can I not make this array a const? it is R/O, not a global.

And, the syntax is cumbersome.

</div>
  • 写回答

1条回答 默认 最新

  • douxi1968 2015-03-31 10:45
    关注

    From the specs:

    Constants

    There are boolean constants, rune constants, integer constants, floating-point constants, complex constants, and string constants.

    IOW, in Go no {struct,array,slice,map,interface,pointer} constants exists.

    评论

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥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添加列问题