string to literal for function exists

Showing 1-7 of 7 messages
string to literal for function exists RoboTamer 8/25/12 7:35 PM
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
  
Thanks

package main
import "reflect"
import "runtime"
//import "strconv"
func funcIs(f string) bool{
    ff := f + "." + f
    if runtime.FuncForPC(reflect.ValueOf(main).Pointer()).Name() == ff {
        return true
    }
    return false
}
func main() {
    if funcIs("main") {
        print("Is type func")
    } else {
        print("Is not type func")
    }
}























Re: string to literal for function exists Andy Balholm 8/25/12 8:07 PM
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.
Re: [go-nuts] string to literal for function exists Jesse McNelis 8/25/12 11:41 PM
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
Re: [go-nuts] string to literal for function exists Rémy Oudompheng 8/25/12 11:46 PM
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.
Re: [go-nuts] string to literal for function exists RoboTamer 8/26/12 4:52 AM
Thanks, this might just work, I can create this map on the fly from a file or database 
Re: [go-nuts] string to literal for function exists mikespook 8/26/12 6:42 AM
于 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/
Re: [go-nuts] string to literal for function exists RoboTamer 8/26/12 8:23 PM


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


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


Thank you mikespook, this is it