2 回答

TA貢獻1784條經驗 獲得超9個贊
我不確定,但我會用 if-else 方式來做
func HandleSomething(w http.ResponseWriter, r *http.Request) {
if someCondition != true {
Error(w, 500, "Some error occurred!")
} else if someOtherCondition != true {
Error(w, 500, "Some other error occurred!")
} else if yetAnotherCondition != true {
Error(w, 500, "Yet another error occurred!")
} else if andFinallyOneLastCondition != true {
Error(w, 500, "One final error occurred!")
} else {
// All good! Send back some real data.
w.WriteJson(someObject)
}
}

TA貢獻1806條經驗 獲得超5個贊
C++ 允許這樣做。它對模板非常方便。
但正如你所見,Go 沒有。我不確定確切的步驟,但這似乎是您可以要求添加到 Go 中的東西,也許它可以用于 1.7。
至于現在的解決方案,也許您可以返回一個未使用的error
. 只需將其nil
從您的Error
函數中返回即可。
- 2 回答
- 0 關注
- 247 瀏覽
添加回答
舉報