1  

This line will panic if there's no error decoding the JSON:

如果解码JSON没有错误,此行将会出现紧急情况:

fmt.Printf("Received command: %v (Error: %s)\n", command, err.Error())

If err == nil, then err.Error() panics with nil pointer derference. Change the line to:

如果err == nil,那么err.Error()会出现nil指针偏差。将行更改为:

fmt.Printf("Received command: %v (Error: %v)\n", command, err)

If you are reading a socket, then there's no guarantee that s.Read() will read a complete JSON value. A better way to write this function is:

如果您正在读取套接字,则无法保证s.Read()将读取完整的JSON值。编写此函数的更好方法是:

func translateMessages(s socket) {
  d := json.NewDecoder(s)
  for {
      fmt.Printf("Waiting for a message ... \n")
      var command map[string]interface{}
      err := d.Decode(&command)
      fmt.Printf("Received command: %v (Error: %v)\n", command, err)
      if err != nil {
        return
      }
  }
}

If you are working with websockets, then you should use the gorilla/webscoket package and ReadJSON to decode JSON values.

如果您正在使用websockets,那么您应该使用gorilla / webscoket包和ReadJSON来解码JSON值。