0
我遇到過不同的方法將變量合併到Go中的錯誤消息中。在下面的例子中,哪種方式是慣用的?有更好的選擇嗎?在錯誤消息中包含數據的慣用方式是什麼?
當事情開始中斷時更安全嗎?例如,當剩餘的內存很少時,分配較少字節的選項將更可取。
如果我們需要產生很多錯誤,哪個更快?
完整的可運行代碼可以在Go Play Space或官方Go Playground中看到。
func f() error {
return SepError("Sepuled " + strconv.Itoa(n) + " sepulcas " + strconv.Itoa(t) +
" times each")
}
func g() error {
return SepError(strings.Join([]string{
"Sepuled", strconv.Itoa(n), "sepulcas", strconv.Itoa(t), "times each"}, " "))
}
func h() error {
return SepError(fmt.Sprintf("Sepuled %d sepulcas %d times each", n, t))
}