how declare constant array?

18,381 views
Skip to first unread message

Norio TANAKA

unread,
Apr 13, 2010, 5:45:28 AM4/13/10
to golang-nuts
I'm a beginner of Go programinng.
When I code

  const arrayConst = [2]int {1, 2}

then, Go compiler said

  const initializer must be constant


How can I declare the constant array?

N.Tanaka

chris dollin

unread,
Apr 13, 2010, 5:53:30 AM4/13/10
to Norio TANAKA, golang-nuts
You can't. Declare a variable instead.

(Go consts are things that are evaluated at compile-time.
Array values don't exist at compile-time. There are no
immutable-variables either, although I don't really
see why not: variables that are not assignable [1]
outside their declaring package seem like a useful
feature to me.)

[1] And hence can't have their addresses taken either [2]

[2] Have const and non-const pointers seems to make life
    harder, not easier ...

--
Chris "allusive" Dollin

Ian Lance Taylor

unread,
Apr 13, 2010, 10:22:34 AM4/13/10
to chris dollin, Norio TANAKA, golang-nuts
chris dollin <ehog....@googlemail.com> writes:

> There are no
> immutable-variables either, although I don't really
> see why not: variables that are not assignable [1]
> outside their declaring package seem like a useful
> feature to me.)

That particular feature is available, in a sense, by giving the
variable a name starting with a lower case letter and providing an
exported function which returns the value of the variable.

Ian

chris dollin

unread,
Apr 13, 2010, 10:41:38 AM4/13/10
to Ian Lance Taylor, Norio TANAKA, golang-nuts

And yet one has to write out the completely stylised method
definition every time. I suppose I'd be content with a feature
that said "make a getter for this unexported variable", and
put up with using method syntax to access it.

I note an implementation issue with this idea, however.
Suppose struct Spoo has an unexported field babylon.
I write a getter

  func (s Spoo) Babylon1() { return s.babylon }

Hmm. If s is moderately large, it all has to be copied to
Babylon1 just so that most of it can be thrown away. OK,

  func (s *Spoo) Babylon2() { return s.babylon }

Now if the caller invokes

  var t Spoo = someSpooExpression()
  t.Babylon2()

and t would have been stack-allocated, it's now likely to be
heap-allocated, because its address has been taken. This
is perhaps not the price one expected to pay for a read-only
field access.

If the compiler were known (or expected in not too long a
term) to inline methods like the Babylon1, and eliminate
the redundant copy; or to inline the second, and eliminate
the redundant address-taking; or to note for Babylon2
that the address doesn't escape from the method and so t
doesn't need to be promoted into the heap; then, OK, using
a method to get the effect of readonly exported fields is
not unreasonable.

The cheap alternative is to export the field with a big
red stamp on it saying NOT FOR UPDATE. I'm not keen.

--
Chris "couldn't manage five examples" Dollin

Norio TANAKA

unread,
Apr 13, 2010, 11:49:59 AM4/13/10
to chris dollin, golang-nuts
Thanks a lot.

One more question.
How can I access variable array as constant array like
,in 'C', const int* pArray?

Best regards,
N.Tanaka

>---html-part included links-------
>mailto:nt9...@gmail.com

chris dollin

unread,
Apr 13, 2010, 12:23:11 PM4/13/10
to Norio TANAKA, golang-nuts
On 13 April 2010 16:49, Norio TANAKA <nt9...@gmail.com> wrote:
Thanks a lot.

One more question.
How can I access variable array as constant array like
,in 'C', const int* pArray?

You can't, not in those terms.

If you really want to protect an array from being updated,
only allow access to it through functions and methods.

Chris

--
Chris "allusive" Dollin
Reply all
Reply to author
Forward
0 new messages