-3

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.

James Henstridge
  • 42,244
  • 6
  • 132
  • 114
  • 2
    as to 2D syntax: `var foo = [2][2]int{{1, 2}, {3, 4}}` – thwd Mar 31 '15 at 09:48
  • The preferred answer gives no insight. The next answer indicates a compiler error: it's an invalid const, but does not indicate the purpose of not allowing array constants, which seems pretty obvious as necessary. And that's not the compiler error in any event: it's an invalid syntax error, not an invalid constant. And neither answer the question of why the poor compiler syntax, nor why arrays can not be constants. These are not very satisfying answers, IMO. – user3431262 Mar 31 '15 at 10:21

1 Answers1

2

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.