http://play.golang.org/p/41MuXxwkAS
On May 15, 5:15 pm,
niklas.v...@gmail.com wrote:
> Hello gophers,
>
> I recently started learning Go and wanted to write a small
> WebSocket-Framework. I finished a basic version
> of the framework calling functions associated with events and delivering
> the sent data as interface{}.
>
> To illustrate, what I mean, this is the current API design:
>
> func echo(conn *Connection, data *map[string]interface{}) {
> fmt.Println(data["test"].(string)) // example data access
> conn.Emit("echoSuccess", data)
>
> }
>
> func main() {
>
> myrouter := NewRouter()
> myrouter.On("echo", echo)
>
> http.Handle("/", http.FileServer(http.Dir("./public")))
> http.Handle("/ws", myrouter.Handler())
>
> if err := http.ListenAndServe(*address, nil); err != nil {
> log.Fatal("ListenAndServe:", err)
>
> }
> }
>
> On the client-side I can then simply emit the echo event and send data
> along with it.
>
> What I dislike about this approach is how the data in the echo function is
> handled. Since
> I am using JSON I could also pass a json.RawMessage to the echo function
> instead of a
> map[string]interface{}, but unmarshalling it for every type of event is
> also a very repetitive
> approach. Although I could pass in then struct and access it more properly.
>
> Provisional nice API design:
> type EchoData struct {
> msg string
>
> }
>
> func echo(conn *Connection, data *EchoData) {
> fmt.Println(data.msg) // example data access
> conn.Emit("echoSuccess", data)
>
> }
>
> So the quesion is basically is there a way to determine the argument types
> of a function using reflection?
> I would then simply save the type information and try to unmarshall the
> received data accordingly.
>
> Thanks in advance,
> Niklas