Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: spaces instead of zeroes in padding when precision is 0 #34003

Closed
inliquid opened this issue Sep 1, 2019 · 3 comments
Closed

fmt: spaces instead of zeroes in padding when precision is 0 #34003

inliquid opened this issue Sep 1, 2019 · 3 comments

Comments

@inliquid
Copy link

inliquid commented Sep 1, 2019

I read fmt docs several times, but didn't find any reasoning why the following would ever happen.

So here is normal working behavior:

func main() {
	i := 1
	fmt.Printf("%03d", i)
}

Output (ok):

001

Now, we add one additional dot . making precision equal to zero, and we'll see spaces instead of zeroes as it should be with 0 flag:

func main() {
	i := 1
	fmt.Printf("%03.d", i)
}

Output (wrong):

  1

System details

go version go1.12.9 windows/amd64
GOARCH="amd64"
GOBIN=""
GOCACHE="C:\Users\***\AppData\Local\go-build"
GOEXE=".exe"
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="windows"
GOOS="windows"
GOPATH="C:\Users\***\go"
GOPROXY=""
GORACE=""
GOROOT="C:\Go"
GOTMPDIR=""
GOTOOLDIR="C:\Go\pkg\tool\windows_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="NUL"
GOROOT/bin/go version: go version go1.12.9 windows/amd64
GOROOT/bin/go tool compile -V: compile version go1.12.9
@KarthikNayak
Copy link
Contributor

Did a little deep diving and found this in the source code.

// Two ways to ask for extra leading zero digits: %.3d or %03d.
// If both are specified the f.zero flag is ignored and
// padding with spaces is used instead.

This seems a little obsolete since the code below is modified, but the gist from the code is that when you use a precision value, only space padding is done. Happy to submit a patch if this isn't intended behavior

@robpike
Copy link
Contributor

robpike commented Sep 1, 2019

It's working as intended and as explained in this comment. It is intentional, but a historical artifact: C's printf works the same way.

@inliquid
Copy link
Author

inliquid commented Sep 1, 2019

I see, thank you for clarifying.

@inliquid inliquid closed this as completed Sep 1, 2019
@golang golang locked and limited conversation to collaborators Aug 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants