在Go中編寫Web服務器時,我希望能夠在運行時取消引用符號,以使我能夠從配置文件中找出哪些函數要調用,如調用在下面的例子中虛構的「eval」函數。這將允許我從處理程序庫中選擇處理程序,並僅使用配置文件部署新的服務器。 Go有什麼方法可以完成這個任務嗎?運行時訪問Go中的符號
config.json
{ "url": "/api/apple", "handler": "Apple", "method": "get" }
{ "url": "/api/banana", "handler": "Banana", "method": "get" }
play.go
package main
import (
"github.com/gorilla/mux"
"net/http"
"encoding/json"
"log"
)
type ConfigEntry struct {
URL string `json:"url"`
Method string `json:"method"`
Handler string `json:"handler"`
}
func main() {
ifp, err := os.Open("config.json")
if err != nil {
log.Fatal(err)
}
dec := json.NewDecoder(ifp)
r := mux.NewRouter()
for {
var config ConfigEntry
if err = dec.Decode(&m); err == io.EOF {
break
} else if err != nil {
log.Fatal(err)
}
r.HandleFunc(config.URL, eval(config.Handler + "Handler")).Methods(config.Method)
}
http.Handle("/", r)
http.ListenAndServe(8080, nil)
}
func AppleHandler(w http.ResponseWriter, r *http.Request) (status int, err error) {
w.Write("Apples!\n")
return http.StatusOK, nil
}
func BananaHandler(w http.ResponseWriter, r *http.Request) (status int, err error) {
w.Write("Bananas!\n")
return http.StatusOK, nil
}
我不認爲這是不可能沒有'map [string] http.Handler'。 –
[在Golang中調用帶有特殊前綴或後綴的所有函數]的可能副本(http://stackoverflow.com/questions/37384473/call-all-functions-with-special-prefix-or-suffix-in-golang)。 – icza