I get a json message from a websocket an the json string is received ok. Then I call json.Unmarshal an get a runtime panic. I looked through the other examples, but this seems to be something else. Here is the code:
func translateMessages(s socket) {
message := make([]byte,4096)
for {
fmt.Printf("Waiting for a message ...
")
if n, err := s.Read(message); err == nil {
command := map[string]interface{}{}
fmt.Printf("Received message: %v (%d Bytes)
", string(message[:n]), n)
err := json.Unmarshal(message[:n],&command)
fmt.Printf("Received command: %v (Error: %s)
", command, err.Error())
}
}
}
And this is the output:
Waiting for a message ...
Received message: {"gruss":"Hello World!"} (24 Bytes)
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xb code=0x1 addr=0x20 pc=0x401938]
goroutine 25 [running]:
runtime.panic(0x6f4860, 0x8ec333)
Any hint what that could be?