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: %x format should not call .String() before hex for [...]byte or []byte #68141

Closed
trim21 opened this issue Jun 23, 2024 · 2 comments
Closed

Comments

@trim21
Copy link

trim21 commented Jun 23, 2024

Go version

go 1.22.4

Output of go env in your module/workspace:

set GO111MODULE=on
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Trim21\AppData\Local\go-build
set GOENV=C:\Users\Trim21\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Trim21\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Trim21\go
set GOPRIVATE=
set GOPROXY=https://goproxy.cn,direct
set GOROOT=C:/Users/Trim21/.g/versions/1.22.4
set GOSUMDB=sum.golang.org
set GOTMPDIR=C:\Users\Trim21\AppData\Local\Temp\go
set GOTOOLCHAIN=local
set GOTOOLDIR=C:\Users\Trim21\.g\versions\1.22.4\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\Trim21\proj\app\go.mod
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\Trim21\AppData\Local\Temp\go\go-build1991184521=/tmp/go-build -gno-record-gcc-switches

What did you do?

package main

import (
	"crypto/sha1"
	"encoding/hex"
	"fmt"
)

type WithString []byte

func (h WithString) String() string {
	return hex.EncodeToString(h[:])
}

type WithOutString []byte

func main() {
	v := sha1.Sum([]byte("hello world"))

	var id WithString = v[:]
	var id2 WithOutString = v[:]

	// 32616165366333356339346663666234313564626539356634303862396365393165653834366564
	fmt.Printf("%X\n", id)

	// 32616165366333356339346663666234313564626539356634303862396365393165653834366564
	fmt.Printf("%x\n", id)

	// 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
	fmt.Printf("%s\n", id)

	fmt.Printf("%X\n", id2) // 2AAE6C35C94FCFB415DBE95F408B9CE91EE846ED
	fmt.Printf("%x\n", id2) // 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
	fmt.Printf("%s\n", id2) // *�l5�Oϴ���_@�����F�
}

What did you see happen?

you can see %x for WithString is double hexed here, which is not expected

32616165366333356339346663666234313564626539356634303862396365393165653834366564
32616165366333356339346663666234313564626539356634303862396365393165653834366564
2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
2AAE6C35C94FCFB415DBE95F408B9CE91EE846ED
2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
*�l5�Oϴ���_@�����F�

What did you expect to see?

should have same result with hex.EncodeToString here

@trim21 trim21 changed the title fmt: %x format should not hex after calling .String() before hex for [...]byte fmt: %x format should not call .String() before hex for [...]byte Jun 23, 2024
@gabyhelp
Copy link

Similar Issues

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@trim21 trim21 changed the title fmt: %x format should not call .String() before hex for [...]byte fmt: %x format should not call .String() before hex for [...]byte or []byte Jun 23, 2024
@seankhliao
Copy link
Member

Duplicate of #21535

@seankhliao seankhliao marked this as a duplicate of #21535 Jun 23, 2024
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Jun 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants