Skip to content

Instantly share code, notes, and snippets.

@apg
Created July 9, 2012 17:43
Show Gist options
  • Save apg/3077832 to your computer and use it in GitHub Desktop.
Save apg/3077832 to your computer and use it in GitHub Desktop.
playing with go reflection, for more declarative msgpack RPC server creation?
package main
import (
"fmt"
"reflect"
)
type Server struct {
name string
}
func (srv *Server) H_id(i int) int {
return i
}
func (srv *Server) H_times_10(i int) int {
return i * 10
}
func (srv *Server) H_neg(i int) int {
return i * -1
}
func main() {
methods := []string {"id", "times_10", "neg"}
foo := &Server{name: "foo"}
value := reflect.ValueOf(foo)
for _, m := range methods {
meth := value.MethodByName("H_" + m)
fmt.Printf("%s(%d) - %v\n", m, 1, meth.Call([]reflect.Value{reflect.ValueOf(1)}))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment