On Tue, Jan 14, 2020 at 10:27 AM 'Vinay Sajip' via golang-nuts
<golang-nuts@googlegroups.com> wrote:
>
> I have a string value that I’d like to convert into an actual object in my 
> running program. The string would be read from a configuration file. For 
> example, the string "os:Stdin" should be processable somehow to get the 
> actual variable os.Stdin. Is that possible in Go? The reflect package seems 
> to only work on objects you already have; I’ve also had a look at the 
> go/importer package, but that seems to primarily deal with source-level 
> information and doesn’t seem to allow you to bridge from there to already 
> existing objects in memory. Other-language equivalents would be 
> Class.forName(...) in Java or Assembly.GetType(...) in C#. Can anyone supply 
> any useful pointers? I know about the plugin package, but AFAIK it doesn’t 
> work on Windows.

In general this is not possible in Go.  Sorry.

If you can restrict yourself to exported package-scope variables, then
you can probably use the debug/pe (on Windows) package to look up the
symbol name and use that value as the variable's address.

Or in some cases you can keep a registry of names that you need to find.

Note that by default the Go linker will discard unreferenced variables
and functions, so the fact that a variable exists in the package
doesn't mean it will exist in the final linked executable.

Ian

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CAOyqgcW7ZBw2skQ%2BFX9_6hcrOtcOhsmGq3nJh-2Xs%3DYQcfXh-w%40mail.gmail.com.

Reply via email to