Dynamic function call in Go
25 Jul 2018Two ways to do it
package main
import (
"encoding/json"
"io"
"os"
"reflect"
"fmt"
)
type A struct {
Name string
Value int
}
type B struct {
Name1 string
Name2 string
Value float64
}
func doA() *A {
return &A{"Cats", 10}
}
func doB() *B {
return &B{"Cats", "Dogs", 10.0}
}
func doC() {
fmt.Printf("c")
}
func Generic(w io.Writer, fn interface{}) {
result := reflect.ValueOf(fn).Call([]reflect.Value{})[0].Interface()
json.NewEncoder(w).Encode(result)
}
func main() {
funcA := doA
Generic(os.Stdout, funcA)
Generic(os.Stdout, doB)
fmt.Printf("other way")
funcs := map[string]func() {"doC": doC}
funcs["doC1"]()
}