是否有更內置的包裝器來使函數返回(X, error)成功執行或中止的功能,例如regexp.MustCompile?我說的是這樣的東西,但更多的是“內置”的。
3 回答

紅糖糍粑
TA貢獻1815條經驗 獲得超6個贊
我認為內置機制沒有意義,因為您可以很好地以各種方式處理非nil錯誤,就像模板包本身中的示例一樣:參見“ text/template/examplefiles_test.go”,說明了' err'的2種不同用法:
// Here starts the example proper.
// T0.tmpl is the first name matched, so it becomes the starting template,
// the value returned by ParseGlob.
tmpl := template.Must(template.ParseGlob(pattern))
err := tmpl.Execute(os.Stdout, nil)
if err != nil {
log.Fatalf("template execution: %s", err)
}
// Output:
// T0 invokes T1: (T1 invokes T2: (This is T2))
在輔助函數的特定情況下(*Template) Must(),將錯誤轉換為異常(緊急)并非總是適用于所有go程序的正確方法,并且涵蓋所有可能的處理錯誤的方式將意味著創建很多“內置”機制。
- 3 回答
- 0 關注
- 213 瀏覽
添加回答
舉報
0/150
提交
取消