Mostafa Talebi
Mostafa Talebi

Reputation: 9173

Casting a string to a func type in GoLang

I have a string which is the name of a function in GoLang. I want to treat them as function. How should I do this? I tried to achieve it through reflect.* but I didn't find a valid path for my purpose.

I get the name fo handlers in a JSON file, and I want to execute those handlers. Something like this:

{
  "students/show" : "ShowStudents",
  "students/add" : "AddStudents"
}

Then I want to execute ShowStudents(), but don't know how to treat it like a variable of type func

Upvotes: 1

Views: 1442

Answers (2)

Himanshu Patel
Himanshu Patel

Reputation: 99

Your task can be broken down into two steps:

  1. Extract function names
  2. Run those functions (assuming that they are defined somewhere)

For step 1, I would unmarshal the JSON into a map[string]string, something like this:

b, err := ioutil.ReadFile(fname)
mp := make(map[string]string)
json.Unmarshal(b, &mp)

Coming to Step 2. In Go, it's not possible to convert string directly to a function call, but it is possible to enumerate the methods of an object using reflect package. This can be used as a workaround in this case. Instead of writing those functions directly, bind them to a dummy type, something like this:

type T int

func (t T) ShowStudents() {
    fmt.Println("Showing Students")
}

func (t T) AddStudents() {
    fmt.Println("Adding Students")
}

func main() {
    var t T
    reflect.ValueOf(t).MethodByName("ShowStudents").Call(nil)
}

Run this example

Upvotes: 2

Vorsprung
Vorsprung

Reputation: 34297

establish a mapping between the keys in the json file and the functions, then use that to call the functions as they appear in the json

package main

import (
    "encoding/json"
    "fmt"
)

func AddStudents() {
    fmt.Println("woo")
}

func ShowStudents() {
    fmt.Println("lots of students")
}

func main() {
    js := `{
  "students/show" : "ShowStudents",
  "students/add" : "AddStudents"
}`

    lookup := make(map[string]string)
    json.Unmarshal([]byte(js), &lookup)
    dispatch := make(map[string]func())
    dispatch["students/show"] = ShowStudents
    dispatch["students/add"] = AddStudents

    for v, _ := range lookup {
        print(v)
        dispatch[v]()
    }
}

Upvotes: 2

Related Questions