Reflection: How to retrieve index of map

54 views
Skip to first unread message

Mark Bauermeister

unread,
Jul 1, 2019, 3:45:42 PM7/1/19
to golang-nuts
I have the following code, where the TokenMap struct is actually part of another package. 
idMap is not exported and thus not accessible without reflection.

Through reflection I can easily find the value of "int", which is "28".
Now, I'd like to do the opposite though. I'd like to find "28"'s index (i e "int").

How would I go about that?

https://play.golang.org/p/UnMi4gQGrIr

Dan Kortschak

unread,
Jul 1, 2019, 10:43:26 PM7/1/19
to Mark Bauermeister, golang-nuts
You can use the Index method on reflect.Value if it is an integer-
indexable type.

https://play.golang.org/p/07YXRPBMqo6
> --
> You received this message because you are subscribed to the Google
> Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/844522f8-145c-44c1-b382-338c2936d5fb%40googlegroups.com
> .
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages