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{
Login(username, password string) (sessionId int, err error)
HelloWorld(sessionId int) (hi string, err error)
}
What I would like to do is use reflection to implement that interface, translating method calls into RPC calls, Marshaling the input parameters, and Unmarshaling the results back into the output of the method. I know that if I can get a []interface{} of the input parameters I can use reflection to make the service call. However I don't see any way to use reflection to dynamically create a value that would implement the interface by calling my reflection-using functions. Does anyone know of a way to do this, even using unsafe?
Reflection gives you the ability to examine types at runtime. It also allows you to examine, modify, and create variables, functions, and structs at runtime. Reflection in Go is built around three concepts: Types, Kinds, and Values. The reflect package in the standard library is the home for the types and functions that implement reflection in Go.
reflect package. The reflect package implements run-time reflection in Go. The reflect package helps to identify the underlying concrete type and the value of a interface{} variable. This is exactly what we need.
The reflect package is the one that contains all the functions we can use for reflection. Here we will explore some of it. 1. Get the type runtime We can get the type of value runtime using reflect.TypeOf. 2. Getting the value at Runtime 3. Getting the number of fields in a struct NumField returns the number of fields a struct has. 4.
There’s one more thing that you can make using reflection in Go. You can make brand-new structs at runtime by passing a slice of reflect.StructField instances to the reflect.StructOf function. This one is a bit weird; we are making a new type, but we don’t have a name for it, so you can’t really turn it back into a “normal” variable.
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.
It appears that the reflect package will gain the ability to create new arbitrarily typed functions in Go 1.1: reflect.MakeFunc.
(the following added in response to @nemo)
Instead of an interface, one could create a struct type:
type MyService struct{
Login func(username, password string) (sessionId int, err error)
HelloWorld func(sessionId int) (hi string, err error)
}
autorpc.ImplementService(&MyService, MyServiceURL)
session, err := MyService.Login(username, password)
stringout, err := MyService.HelloWorld(session)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With