Thanks Axel On Sunday, September 3, 2017 at 2:30:48 PM UTC+3, Axel Wagner wrote: > > type MathFunc func(int, int) int declares a *new* type, called MathFunc, > with underlying type func(int, int) int, which is why the type-assertion > fails - you are checking whether the type is *exactly* MathFunc, not "has > the same underlying type". > > There are several things you could do, apart from what you mentioned. You > could use reflect and check for assignability > <https://godoc.org/reflect#Type.AssignableTo>. You could type-assert to > the func-type and do the conversion: > f := MathFunc(sym.(func(int, int) int)) > You could support using either via, e.g. > > var f MathFunc > switch v := sym.(type) { > case MathFunc: > f = v > case func(int, int) int: > f = MathFunc(v) > default: > panic("wrong type") > } > > You could (from go1.9 on) make MathFunc an alias: > type MathFunc = func(int, int) int > in which case the two are interchangeable (so the type-assertion succeeds) > and you still get a name for the type for documentation purposes. > > Or you could just forego the MathFunc type altogether and just use > func(int, int) int in your code. > > FWIW, the reason http has HandlerFunc, is mainly to add a method > <http://godoc.org/net/http#HandlerFunc.ServeHTTP> to it to make it an > http.Handler <https://godoc.org/net/http#Handler>, to make it easier to > implement that interface. > > On Sun, Sep 3, 2017 at 11:47 AM, Miki Tebeka <[email protected] > <javascript:>> wrote: > >> Hi, >> >> Assume I have a plugin: >> package main >> >> func Add(x, y int) int { >> return x + y >> } >> >> func main() {} >> >> I'd like to make sure that plugin functions are from certain type. So I >> wrote (ignore panics, they are for brevity): >> Enter code here...package main >> >> import ( >> "fmt" >> "plugin" >> ) >> >> type MathFunc func(int, int) int >> >> func main() { >> p, err := plugin.Open("./plugin.so") >> if err != nil { >> panic(err) >> } >> sym, err := p.Lookup("Add") >> if err != nil { >> panic(err) >> } >> >> >> // fn := sym.(MathFunc) >> fn := sym.(func(int, int) int) >> fmt.Println(fn(1, 2)) >> } >> >> The commented out line causes a panic. >> >> The other way I see is to expose MathFunc to plugins but then plugin >> writers will have to write something like: >> var Add = MathFunc(func(x, y int) int { >> return x + y >> }) >> >> Which is ugly :) >> >> Any way around this? (I saw that http.HandleFunc gets the function >> signature like I do now - so I guess this is the best way, but worth >> checking). >> >> -- >> 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 [email protected] <javascript:>. >> For more options, visit https://groups.google.com/d/optout. >> > >
-- 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 [email protected]. For more options, visit https://groups.google.com/d/optout.
