Re: string to literal for function exists

164 views
Skip to first unread message

Andy Balholm

unread,
Aug 25, 2012, 11:07:20 PM8/25/12
to golan...@googlegroups.com
The reflect package does not support looking up functions by name. The reason for this is that the linker reduces the size of compiled programs by discarding functions that it knows will never be called.

Jesse McNelis

unread,
Aug 26, 2012, 2:41:24 AM8/26/12
to RoboTamer, golan...@googlegroups.com
On Sun, Aug 26, 2012 at 12:35 PM, RoboTamer <grue...@gmail.com> wrote:
> I am working on a function exists, is function function.
> Seams I am almost there, but I can't figure out how to replace main in
> ValueOf(main) with a variable.
> I tried Unquote with no success.
> You can test what I have so far at the play.go

Go doesn't allow this because there are certain advantages to disallowing it.
1. The compiler can identify and remove code that will never be called
at compile time, reducing the size of the executable.
2. You can remove code from your code base and know at compile time
whether or not that code could ever be called at runtime. Making it
easier to refactor code.

If Go allowed you to convert a string in to a function pointer then we
would lose the two advantages above. It's a trade off.

It's common when coming to a new programming language to just keep
trying to write code like you've done in another language. Each
language is different and requires a different approach. If you're
trying to do this in Go then it's likely your coming at your problem
from the perspective of a dynamically typing language programmer and
you should rethink how you'd design your program.

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

Rémy Oudompheng

unread,
Aug 26, 2012, 2:46:13 AM8/26/12
to RoboTamer, golan...@googlegroups.com
On 2012/8/26 RoboTamer <grue...@gmail.com> wrote:
> I am working on a function exists, is function function.
> Seams I am almost there, but I can't figure out how to replace main in
> ValueOf(main) with a variable.
> I tried Unquote with no success.
> You can test what I have so far at the play.go

Packages are not subject to reflection. Use a map to map names to
functions (see template.FuncMap for an example).

Rémy.

RoboTamer

unread,
Aug 26, 2012, 7:52:34 AM8/26/12
to golan...@googlegroups.com, RoboTamer
Thanks, this might just work, I can create this map on the fly from a file or database 

Xing Xing

unread,
Aug 26, 2012, 9:42:11 AM8/26/12
to golan...@googlegroups.com
于 Sun, 26 Aug 2012 04:52:34 -0700 (PDT)
RoboTamer <grue...@gmail.com> 写道:

>
>
> On Saturday, August 25, 2012 11:46:13 PM UTC-7, Rémy Oudompheng wrote:
> >
> > On 2012/8/26 RoboTamer <grue...@gmail.com <javascript:>> wrote:
> > > I am working on a function exists, is function function.
> > > Seams I am almost there, but I can't figure out how to replace
> > > main in ValueOf(main) with a variable.
> > > I tried Unquote with no success.
> > > You can test what I have so far at the play.go
> >
> > Packages are not subject to reflection. Use a map to map names to
> > functions (see template.FuncMap for an example).

"Function call by name in Golang"
http://www.mikespook.com/2012/07/function-call-by-name-in-golang/

RoboTamer

unread,
Aug 26, 2012, 11:23:08 PM8/26/12
to golan...@googlegroups.com


On Sunday, August 26, 2012 6:42:11 AM UTC-7, mikespook wrote:
于 Sun, 26 Aug 2012 04:52:34 -0700 (PDT)


Thank you mikespook, this is it 
Reply all
Reply to author
Forward
0 new messages