区分JSON和其他错误

enc := json.NewEncoder(w)
err := enc.Encode(struct {
    Method    string        `json:"method"`
    Results   []interface{} `json:"results"`
    CacheTime int           `json:"cache_time"`
}{Method: answerInlineQueryMethod, Results: results, CacheTime: 0})
if err != nil {
    log.Printf("failed to answer to inline query: %s", err)
}

How can I distinguish between JSON errors, which should cause a panic and errors caused by sending the response, which should be logged?

The encoding/json package defines the error types it will return. For encoding you have MarshalerError, UnsupportedTypeError, and UnsupportedValueError.

You can inspect if the error type returned by Encode is one of these 3.

If the responses are not huge and don't rely on sending multiple json values per the json.Encoder protocol, you can simply use json.Marshal and write the response separately.