Created
July 9, 2012 17:43
-
-
Save apg/3077832 to your computer and use it in GitHub Desktop.
playing with go reflection, for more declarative msgpack RPC server creation?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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