package events
import (
"sync"
)
var Event map[string]func(ps []string)
var maxDataLock sync.Mutex
func init() {
Event = make(map[string]func(ps []string))
maxDataLock = &sync.Mutex{}
}
// Подписка на событие
func Add(name string, callback func(ps []string)) {
maxDataLock.Lock()
Event[name] = callback
maxDataLock.Unlock()
}
// Вызов события
func Call(name string, ps []string) {
maxDataLock.Lock()
Event[name](ps)
maxDataLock.Unlock()
}
maxDataLock.Lock()
Event[name] = callback
maxDataLock.Unlock()
package events
var Event map[string]func(ps []string)
func init() {
Event = make(map[string]func(ps []string))
}
// Подписка на событие
func Add(name string, callback func(ps []string)) {
Event[name] = callback
}
// Вызов события
func Call(name string, ps []string) {
Event[name](ps)
}
package main
import (
"encoding/json"
"fmt"
"generals/Data/api"
"log"
"net/http"
"github.com/julienschmidt/httprouter"
)
type MyJsonName struct {
ID int `json:"id"`
Method func() `json:"method"`
Params map[string]string `json:"params"`
}
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "->, %s\n", r.URL.Query().Get("data"))
jsonSrc := []byte(r.URL.Query().Get("data"))
var myJson MyJsonName
var mapData map[string]string
json.Unmarshal(jsonSrc, &myJson)
//пытаюсь вызвать метод.
//ошибка
myJson.Method()
}
func main() {
api.Run()
router := httprouter.New()
router.GET("/rpc", Hello)
log.Fatal(http.ListenAndServe(":700", router))
}