將錯誤傳遞到服務器:uid1以下代碼為單個請求返回 3 個 JSON 響應。就我的理解而言,一旦回復被寫給回復作者,它就完成了;這似乎不是真的。我在哪里可以閱讀有關此內容的更多信息,我在這里解決的確切問題是什么?func getJob(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") vars := mux.Vars(r) job_id := vars["job_id"] var job job // basic validation for UUID job_id uid, err := uuid.FromString(job_id) fmt.Println(uid) fmt.Println(err) if _, err := uuid.FromString(job_id); err != nil { sendErrorResponse(w, "Invalid job id "+job_id, err) } if result := db.Where("job_id = ?", uid).First(&job); result.Error != nil { sendErrorResponse(w, "Error retrieving job with "+job_id, result.Error) } json.NewEncoder(w).Encode(job)}func sendErrorResponse(w http.ResponseWriter, message string, err error) { w.WriteHeader(http.StatusInternalServerError) if err := json.NewEncoder(w).Encode(Response{Message: message, Error: err.Error()}); err != nil { panic(err) }}這是郵遞員的輸出:{ "Message": "Invalid job id 1", "Error": "uuid: incorrect UUID length: 1"}{ "Message": "Error retrieving job with 1", "Error": "record not found"}{ "ID": 0, "CreatedAt": "0001-01-01T00:00:00Z", "UpdatedAt": "0001-01-01T00:00:00Z", "DeletedAt": null, "application": "", "status": "", "worker": "", "job_id": "00000000-0000-0000-0000-000000000000"}
對一個請求的多個 JSON 響應
ibeautiful
2022-09-12 16:34:14