Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I create a new function using reflection in Go?

Tags:

reflection

go

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?

like image 487
Matt Avatar asked Sep 12 '12 23:09

Matt


People also ask

What is reflection in Go programming?

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.

What is the use of the reflect package in Golang?

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.

How to use the reflect package in Java?

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.

What can you make with reflection in go?

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.


2 Answers

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.

like image 193
thwd Avatar answered Oct 25 '22 18:10

thwd


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)
like image 38
Matt Avatar answered Oct 25 '22 18:10

Matt