go/types.Typ is a slice-of-pointer-to-struct yet uses map syntax

63 views
Skip to first unread message

Will Faught

unread,
Feb 15, 2017, 2:08:05 PM2/15/17
to golang-nuts
The go/types.Typ decl:

var Typ = []*Basic{
        Invalid:       {Invalid, 0, "invalid type"},

        Bool:          {Bool, IsBoolean, "bool"},
        Int:           {Int, IsInteger, "int"},
        Int8:          {Int8, IsInteger, "int8"},
        ...
}

Typ is a slice of pointer to Basic—got it; Basic is a struct—got it; the {Bool, IsBoolean, "bool"} stuff is clearly a Basic or *Basic—got it; but where is the map syntax coming from? How does that work?

Ian Lance Taylor

unread,
Feb 15, 2017, 2:22:52 PM2/15/17
to Will Faught, golang-nuts
That's not "map syntax", it's general composite literal syntax and is
permitted for all kinds of composite literals. For a slice, it means
that the element with index `Invalid` gets the value `{Invalid, 0,
"invalid type"}`, and so forth. For a slice composite literal the
keys (in this case, `Invalid`, `Bool`, etc.) must all be non-negative
integer constant expressions.

Ian

howar...@gmail.com

unread,
Feb 15, 2017, 2:31:27 PM2/15/17
to golang-nuts
Invalid, Bool, Int, Int8 there are const integers, and they are specifying the entry's position in the slice, allowing gaps in the list to be implicitly defined.

Will Faught

unread,
Feb 15, 2017, 5:12:15 PM2/15/17
to golang-nuts, howar...@gmail.com
Ah, I forgot about that syntax. Thanks!

On Wed, Feb 15, 2017 at 11:31 AM <howar...@gmail.com> wrote:
Invalid, Bool, Int, Int8 there are const integers, and they are specifying the entry's position in the slice, allowing gaps in the list to be implicitly defined.

--
You received this message because you are subscribed to a topic in the Google Groups "golang-nuts" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nuts/k7ZFpDN5eXM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages