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))
}