我从 websocket 收到一条 json 消息,json 字符串接收正常。然后我调用 json.Unmarshal 得到运行时恐慌。我查看了其他示例,但这似乎是另一回事。这是代码:
func translateMessages(s socket) {
message := make([]byte,4096)
for {
fmt.Printf("Waiting for a message ... \n")
if n, err := s.Read(message); err == nil {
command := map[string]interface{}{}
fmt.Printf("Received message: %v (%d Bytes)\n", string(message[:n]), n)
err := json.Unmarshal(message[:n],&command)
fmt.Printf("Received command: %v (Error: %s)\n", command, err.Error())
}
}
}
这是输出:
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)
任何提示可能是什么?
www说
相关分类