-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.

2
  • 2
    as to 2D syntax: var foo = [2][2]int{{1, 2}, {3, 4}} Commented Mar 31, 2015 at 9: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. Commented Mar 31, 2015 at 10:21

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.