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: fmt.Printf("%b", []byte{...}) fails unnecessarily #13478

Closed
nsd20463 opened this issue Dec 4, 2015 · 2 comments
Closed

fmt: fmt.Printf("%b", []byte{...}) fails unnecessarily #13478

nsd20463 opened this issue Dec 4, 2015 · 2 comments
Milestone

Comments

@nsd20463
Copy link
Contributor

nsd20463 commented Dec 4, 2015

Formatting any slice (or array) but []byte in binary using the %b format code works. For example:

fmt.Printf("%d", []int{1,2,3}) -> [ 1 10 11 ]

but if the members of the array or slice are byte then the special case fmtBytes() is invoked, and it lacks a %b case. So instead the formatting fails:

fmt.Printf("%b", []byte{1,2,3}) -> %!b([]uint8=[1 2 3])

Looking at the code in src/fmt/ I don't think it would be hard to not only make %b work on []byte, but to also make it work nicely, like %x and % x do, giving something like:

fmt.Printf("% b", []byte{1,2,3}) -> 00000001 00000010 00000011

which is the binary equivalent of the special case % x operation on a []byte, which, for reference, produces:

fmt.Printf("% x", []byte{1,2,3}) -> 01 02 03

I see this behavior in go 1.5.1 on linux/amd64 and linux/arm, but the behavior isn't new, nor is it specific to those OS/ARCH combinations.

@gopherbot
Copy link

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

@robpike robpike added this to the Go1.7 milestone Dec 4, 2015
@gopherbot
Copy link

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

@golang golang locked and limited conversation to collaborators Mar 28, 2017
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

3 participants