Created
April 8, 2017 07:03
-
-
Save adrianmoses/347906265f629b98e5acd9321e3c60e6 to your computer and use it in GitHub Desktop.
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" | |
"encoding/json" | |
) | |
type Task struct { | |
Name string | |
} | |
func Hello() string { | |
return "Hello!" | |
} | |
func main() { | |
taskMap := make(map[string]interface{}) | |
taskMap["hello"] = Hello | |
task := Task{Name: "hello"} | |
emptyArgs := make([]reflect.Value, 0) | |
reflectedTask := reflect.ValueOf(taskMap[task.Name]) | |
fmt.Println(reflectedTask.Call(emptyArgs)) | |
serialized, err := json.Marshal(task) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("serialized data: ", string(serialized)) | |
var deseralized Task | |
if err := json.Unmarshal(serialized, &deseralized); err != nil { | |
panic(err); | |
} | |
reflectedTask = reflect.ValueOf(taskMap[deseralized.Name]) | |
fmt.Println(reflectedTask.Call(emptyArgs)) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment