golang fmt.Sprintf 中 %d 与 %s 混用会怎样

发布时间: 2022-04-19 16:34:10 作者: 大象笔记

写了一个后台分页的 bug,问题的原因在 fmt.Sprintf 中我将 int 类型传递给了 %s。

有问题的代码

categoryId := 5

if categoryId > 0 {
	url += fmt.Sprintf("?category=%s", categoryId)
}

其输出是:

/solutions?category=%!s(int=5)

导致之后的参数解析逻辑没有正常执行。

而且这个 bug 具有隐蔽性,从打印日志输出看,我看了半天也没有发现这是一个错误的输出。 后来看 golang fmt 的文档,才知道这是错误信息。

Format errors: If an invalid argument is given for a verb, such as providing a string to %d, the generated string will contain a description of the problem, as in these examples: All errors begin with the string "%!" followed sometimes by a single character (the verb) and end with a parenthesized description.

正确的代码

categoryId := 5

if categoryId > 0 {
	url += fmt.Sprintf("?category=%d", categoryId)
}

输出:

/solutions?category=5

这才一切运行正常。

隐蔽的 bug

我一直以为 %s %d 是可以混用的,可能是用 python 时养成的坏习惯。

而 golang 拒绝一切隐式转换,所以就出现了错误信息。 最危险的时,这并没有引起运行时错误,也没有编译时错误,非常难发现。

这时单元测试就非常有必要了。

规避方法

参考

我是一名山东烟台的开发者,联系作者