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: Printf does not zero pad. #11422

Closed
dajohi opened this issue Jun 26, 2015 · 4 comments
Closed

fmt: Printf does not zero pad. #11422

dajohi opened this issue Jun 26, 2015 · 4 comments
Milestone

Comments

@dajohi
Copy link

dajohi commented Jun 26, 2015

The following code does not 0-pad on tip. It does work expected with 1.4.2, see http://play.golang.org/p/A7oXWwj5s_

$ go version
go version devel +a1cc84f Fri Jun 26 09:53:36 2015 +0000 openbsd/amd64

package main

import (
        "fmt"
        "math/big"
)

func main() {
        value := []byte{
                0x89, 0x6c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        }
        targetDifficulty := new(big.Int).SetBytes(value)

        fmt.Printf(" got: %064x\n", targetDifficulty.Bytes())
        fmt.Printf("want: 0000000000000000896c00000000000000000000000000000000000000000000\n")
}
 got: 896c00000000000000000000000000000000000000000000
want: 0000000000000000896c00000000000000000000000000000000000000000000
@griesemer griesemer self-assigned this Jun 26, 2015
@griesemer griesemer added this to the Go1.5Maybe milestone Jun 26, 2015
@griesemer
Copy link
Contributor

Confusing example - there's no need to involve math/big. But you're right, tip appears to be wrong. On the playground: http://play.golang.org/p/pnJg1u_z2K:

package main

import "fmt"

func main() {
    fmt.Printf("%08x\n", []byte{1})
}

we get:

00000001

On tip we get:

01

@griesemer griesemer modified the milestones: Go1.5, Go1.5Maybe Jun 26, 2015
@griesemer griesemer assigned robpike and unassigned griesemer Jun 26, 2015
@robpike
Copy link
Contributor

robpike commented Jun 27, 2015

Given
fmt.Printf("%04x\n", []byte{0xab})
fmt.Printf("% 04x\n", []byte{0xab})
fmt.Printf("%04x\n", []byte{0xab, 0xbc})
fmt.Printf("% 04x\n", []byte{0xab, 0xbc})

1.4 does this:

00ab
00ab
abbc
ab bc

while tip does this:

ab
ab
abbc
ab bc

Both are wrong. The root bug is original; the change in behavior applies because of a fix at tip that, coincidentally, propagates the original bug.

The correct output is

00ab
00ab
00ab00bc
00ab 00bc

I will fix.

@gopherbot
Copy link

CL https://golang.org/cl/11600 mentions this issue.

@mikioh mikioh changed the title fmt.Printf does not zero pad. fmt: Printf does not zero pad. Jun 27, 2015
@robpike
Copy link
Contributor

robpike commented Jun 28, 2015

I was mistaken; there has been a regression from 1.4, where the output was correct:

00ab
00ab
abbc
ab bc

At issue is whether the width and flags apply elementwise or to the whole, and the answer is: width: whole; flags, elementwise. And this must be consistent for []byte and for string when using this format.

The CL listed here restores and explains the behavior.

@golang golang locked and limited conversation to collaborators Jun 28, 2016
@rsc rsc unassigned robpike Jun 23, 2022
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