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.

    评论

报告相同问题?