Help to choose the right type of the key for a map.

190 views
Skip to first unread message

alex-coder

unread,
Mar 15, 2023, 12:16:59 PM3/15/23
to golang-nuts
Hi All !

There is a package named ast, inside there are a lot of structures like:
ast.Comment, ast.Field, ast.Node and so on.
And I whould like to use types of those structure as a keys.
So, I could not figure out what type of the key I should use
in order to store in a map something under the key ? 

I'm in dubt here. Help !

Thank you.

alex-coder

unread,
Mar 15, 2023, 2:41:58 PM3/15/23
to golang-nuts
the ast package it is possible to find there:

thank you.

среда, 15 марта 2023 г. в 19:16:59 UTC+3, alex-coder:

Ian Lance Taylor

unread,
Mar 15, 2023, 4:55:03 PM3/15/23
to alex-coder, golang-nuts
On Wed, Mar 15, 2023 at 9:17 AM alex-coder <a.gus...@gmail.com> wrote:
>
> There is a package named ast, inside there are a lot of structures like:
> ast.Comment, ast.Field, ast.Node and so on.
> And I whould like to use types of those structure as a keys.
> So, I could not figure out what type of the key I should use
> in order to store in a map something under the key ?

I don't really understand the question. But I do know that most of
the types in the go/ast package can't be used as map keys, because
they can't be compared for equality, because they have fields of slice
type. You can't use a slice, or a struct with a field of slice type,
as a map key.

Ian

alex-coder

unread,
Mar 15, 2023, 5:16:13 PM3/15/23
to golang-nuts
Ian, thank you.
but may be the is any workaround ?
Use as a key not directly but like a derived from those interface/structure types ?
Of course I may declare key like string and use switch + .(type) to mimic it.

So, a little bit more.
there is a go standard package named ast. the docs about it you may find there:
there is an arrea where are declared a lot of types:
type ArrayType struct {}
type AssignStmt struct {}
type BadDecl struct {}
and so on and ending with 
type ValueSpec struct {}
in fact there are a lot of them there.
What do I whant is to use those types of interfaces or structures as a key to access to a value in a map.  
So, the question is: what type of a key for a map I must use ?

Thank you. 

среда, 15 марта 2023 г. в 23:55:03 UTC+3, Ian Lance Taylor:

Ian Lance Taylor

unread,
Mar 15, 2023, 5:26:35 PM3/15/23
to alex-coder, golang-nuts
On Wed, Mar 15, 2023 at 2:16 PM alex-coder <a.gus...@gmail.com> wrote:
>
> Ian, thank you.
> but may be the is any workaround ?
> Use as a key not directly but like a derived from those interface/structure types ?
> Of course I may declare key like string and use switch + .(type) to mimic it.

Sure, convert to a string and use that as a map key.

By the way, I should say that although you can't use `ast.Field` as a
map key type, you can use `*ast.Field`. Of course then two different
Field values that happen to look exactly the same will get different
map entries. So it kind of depends on what you want to do.

Ian

alex-coder

unread,
Mar 16, 2023, 6:24:49 PM3/16/23
to golang-nuts
>> Sure, convert to a string and use that as a map key.
:-) no, no, no. it looks very very ugly.

четверг, 16 марта 2023 г. в 00:26:35 UTC+3, Ian Lance Taylor:

Andy Balholm

unread,
Mar 16, 2023, 7:12:22 PM3/16/23
to golang-nuts
I would guess that in the case of AST nodes, you're likely more interested in the identity of the nodes than their values. In that case, you should use the pointers to the nodes as your map keys.

If the values truly are what you care about, use the go/format package to convert the node back to source code, and use the resulting string as your map key.

alex-coder

unread,
Mar 17, 2023, 8:01:31 AM3/17/23
to golang-nuts
Andy, thank you.

In case there would be a choice between ugly and achievable, I must take the last one:-)

Another thing I have found it that it is impossible to use type as a parameter when you call to a function.

Thank you.

пятница, 17 марта 2023 г. в 02:12:22 UTC+3, Andy Balholm:

alex-coder

unread,
Mar 18, 2023, 8:00:06 AM3/18/23
to golang-nuts
Ian and Andy,
looks like the to string conversion works and even does not look ugly. :-)

Thank you so much.

пятница, 17 марта 2023 г. в 15:01:31 UTC+3, alex-coder:
Reply all
Reply to author
Forward
0 new messages