英文:
fmt.Printf() flag '0' is not ignored for strings
问题
根据文档,对于字符串,标志'0'会被忽略。
但是在下面的代码中,标志'0'并没有被忽略。文档是错误的吗?还是我误解了?
package main
import "fmt"
func main() {
	fmt.Printf("%05s", "abc")
	// 输出 00abc
}
英文:
According to the doc, the flag '0' is ignored for strings
> '0'	pad with leading zeros rather than spaces;
for numbers, this moves the padding after the sign;
ignored for strings, byte slices and byte arrays
but the flag '0' is not ignored in below code. Is the doc wrong? Or do I misunderstand it?
package main
import "fmt"
func main() {
	fmt.Printf("%05s", "abc")
	// print 00abc
}
答案1
得分: 4
看起来你发现了一个 bug。
源代码 在只有 -(减号)标志时重置了 zero 标志。对于字符串和其他任何类型,它都没有被修改。
而且,输出字符串的函数 也没有重置 zero 标志。
英文:
Looks like you found a bug.
The source code resets the zero flag only for - (minus) flag. It is not modified neither for strings nor for any other type.
And the function that outputs a string doesn't reset the zero flag either.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论