Can I create a new function using reflection in Go?

前端 未结 4 1683
醉话见心
醉话见心 2021-02-07 20:44

I have an idea to use interfaces in Go to define RPC style interfaces. So for a given service, I might create an interface like this:

type MyService interface{
          


        
4条回答
  •  醉酒成梦
    2021-02-07 21:19

    You can not create a type with attached methods via reflection, as to instantiate an object of that type.

    You could possibly achieve this with a lot of hackery through the unsafe package. But even then, it'd be a massive pain.

    If you elaborated more on the problem you're trying to solve, the community could come up with alternatives ways of solving it.

    Edit (23. July 2015): starting with Go 1.5 there's reflect.FuncOf and reflect.MakeFunc, which do exactly what you want.

提交回复
热议问题