fmt.Printf() 函数中的 flag ‘0’ 对于字符串不会被忽略。

huangapple go评论80阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2022年10月29日 22:59:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/74246342.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定