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 does not format byte slices nicely #34916

Closed
guysoffer opened this issue Oct 15, 2019 · 4 comments
Closed

fmt does not format byte slices nicely #34916

guysoffer opened this issue Oct 15, 2019 · 4 comments
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.

Comments

@guysoffer
Copy link

guysoffer commented Oct 15, 2019

What version of Go are you using (go version)?

go version go1.12.1 windows/amd64

Does this issue reproduce with the latest release?

reproduces on go playground

What operating system and processor architecture are you using (go env)?

go env Output
C:\Users\gs082r>go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\gs082r\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Users\gs082r\go
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\gs0
82r\AppData\Local\Temp\go-build177268958=/tmp/go-build -gno-record-gcc-switches

What did you do?

I'm trying to print a slice of bytes as an array of hex values.

What did you expect to see?

[0x0a 0x14 0x1e 0x28 0x32 0x3c 0x46 0x50 0x5a 0x64 0x65]

What did you see instead?

0x0a14

Please see playground link:
https://play.golang.org/p/gQIYJV6ogx4

The first line is ok for a slice of ints.. the %.2v is a sort of workaround, but not what I actually need.

dump from playground:

package main

import (
	"fmt"
)

func main() {
	slcInt8 := [...]int8{10,20,30,40,50,60,70,80,90,100,101}
	fmt.Printf("%#.2x\n", slcInt8)	
	slcByte := [...]byte{10,20,30,40,50,60,70,80,90,100,101}
	fmt.Printf("%#.2x\n", slcByte)	
	slcUint8 := [...]byte{10,20,30,40,50,60,70,80,90,100,101}
	fmt.Printf("%#.2x\n", slcUint8)	
	
	fmt.Printf("%#x\n", slcInt8)	
	fmt.Printf("%#x\n", slcByte)	
	fmt.Printf("%#x\n", slcUint8)	
	
	fmt.Printf("%#.2v\n", slcInt8)	
	fmt.Printf("%#.2v\n", slcByte)	
	fmt.Printf("%#.2v\n", slcUint8)		
}

output:

[0x0a 0x14 0x1e 0x28 0x32 0x3c 0x46 0x50 0x5a 0x64 0x65]
0x0a14
0x0a14
[0xa 0x14 0x1e 0x28 0x32 0x3c 0x46 0x50 0x5a 0x64 0x65]
0x0a141e28323c46505a6465
0x0a141e28323c46505a6465
[11]int8{10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 101}
[11]uint8{0x0a, 0x14, 0x1e, 0x28, 0x32, 0x3c, 0x46, 0x50, 0x5a, 0x64, 0x65}
[11]uint8{0x0a, 0x14, 0x1e, 0x28, 0x32, 0x3c, 0x46, 0x50, 0x5a, 0x64, 0x65}
@ianlancetaylor
Copy link
Contributor

I'm sorry, I don't understand what you are asking for. It seems that using %#.2x gives you exactly what you want.

Whatever we think of the behavior of the fmt package (and I think it is making a reasonable choice here) we can't change it now. It would break too many existing programs.

@ianlancetaylor ianlancetaylor added the WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided. label Oct 15, 2019
@martisch
Copy link
Contributor

martisch commented Oct 15, 2019

fmt.Printf("[%# x]", slcByte) will output:

[0x0a 0x14 0x1e 0x28 0x32 0x3c 0x46 0x50 0x5a 0x64 0x65]

@guysoffer
Copy link
Author

@ianlancetaylor

slcByte := [...]byte{10,20,30,40,50,60,70,80,90,100,101}
fmt.Printf("%#.2x\n", slcByte)

gives output:
0x0a14

does this look like expected/reasonable behaviour ?

@martisch thanks ! this is exactly what I was looking for !

@martisch
Copy link
Contributor

martisch commented Oct 16, 2019

The current behavior looks to me as documented in package fmt. Note that []byte often formats the way a string would be formatted. fmt.Printf("%#.2x\n",string([]byte{10,20,30,40,50,60,70,80,90,100,101})) gives: 0x0a14

That is why []int behaves differently. We can not change it without breaking backwards compatibility. Since there is a way to format the slcByte the way wanted I am closing the issue.

If the behavior should be changed please open a new issue with a concrete proposal for Go 2 how the behavior of fmt should be changed and how the gained advantage is worth breaking the backwards compatibility.

@golang golang locked and limited conversation to collaborators Oct 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge WaitingForInfo Issue is not actionable because of missing required information, which needs to be provided.
Projects
None yet
Development

No branches or pull requests

4 participants