Is there anything similar to Class.forName("string") in Java or "string".constantize in Ruby for Go?

920 views
Skip to first unread message

daniel....@gmail.com

unread,
Sep 26, 2013, 6:24:13 AM9/26/13
to golan...@googlegroups.com
Hi,

Basically the idea is to find something similar to Java's Class.forName("some_type") or Ruby's "some_type".constantize in order to get a type constant at runtime. I know I could implement this behaviour with a switch/case statement, but for the purposes of the project I am working on, it would be a bit repetitive.

A more detailed example of what I am trying to accomplish here: http://play.golang.org/p/sJyCSf8anE

Thanks!

Javier Asensio

unread,
Sep 26, 2013, 7:09:30 AM9/26/13
to daniel....@gmail.com, golang-nuts
I presume you can't (I'm fairly new to go), I didn't find anything similar in the reflect api. Anyway, from my point of view that would too "magical" for my taste, how about making a registry of the available services that you can actually instantiate? I think it would be easier to code/test than using reflection, which always adds an extra layer of complexity

http://play.golang.org/p/72NoDDLZR9

cheers,

Javi


--
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.
For more options, visit https://groups.google.com/groups/opt_out.

Jesse McNelis

unread,
Sep 26, 2013, 8:47:48 AM9/26/13
to daniel....@gmail.com, golang-nuts
On Thu, Sep 26, 2013 at 8:24 PM, <daniel....@gmail.com> wrote:
Hi,

Basically the idea is to find something similar to Java's Class.forName("some_type") or Ruby's "some_type".constantize in order to get a type constant at runtime. I know I could implement this behaviour with a switch/case statement, but for the purposes of the project I am working on, it would be a bit repetitive.

Types that you don't explicitly reference in code aren't included in the binary at runtime and thus can't be referred to. If getting a type from a string was possible it would prevent the compiler from doing this dead code elimination which would make statically linked binaries huge.

If you want a to convert a string to a type at runtime you'll need to have a map[string]reflect.Type that you explicitly fill.




 

A more detailed example of what I am trying to accomplish here: http://play.golang.org/p/sJyCSf8anE

Thanks!

--
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.
For more options, visit https://groups.google.com/groups/opt_out.



--
=====================
http://jessta.id.au

daniel....@gmail.com

unread,
Sep 26, 2013, 1:42:46 PM9/26/13
to golan...@googlegroups.com, daniel....@gmail.com, jes...@jessta.id.au
Thanks for clarifying it. I went with Javi's suggestion.

George McBay

unread,
Sep 27, 2013, 7:02:22 PM9/27/13
to golan...@googlegroups.com, daniel....@gmail.com, jes...@jessta.id.au
Also keep in mind that Go is very good at parsing itself, so it isn't too difficult to write a program that you can point at an arbitrary package directory (or set of such directories) to automatically generate a name->Type map for you and just make that part of your build process.
Reply all
Reply to author
Forward
0 new messages