Skip to content

fmt: state that "%q" verb should not be used to print int value #78486

@leonklingele

Description

@leonklingele

Go version

go version go1.26.1 darwin/arm64

Output of go env in your module/workspace:

AR='ar'
CC='clang'
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_ENABLED='0'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
CXX='clang++'
GCCGO='gccgo'
GO111MODULE=''
GOARCH='arm64'
GOARM64='v8.0'
GOAUTH='netrc'
GOBIN=''
GOCACHE=''
GOCACHEPROG=''
GODEBUG=''
GOENV=''
GOEXE=''
GOEXPERIMENT=''
GOFIPS140='off'
GOFLAGS=''
GOGCCFLAGS='-fPIC -arch arm64 -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/sl/yg232rm16b74lh69dhhscfz40000gn/T/go-build3024692398=/tmp/go-build -gno-record-gcc-switches -fno-common'
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMOD=''
GOMODCACHE=''
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH=''
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/nix/store/kh43nhaz1qcpwws2xq805lrmwpmn9i3k-go-1.26.1/share/go'
GOSUMDB='sum.golang.org'
GOTELEMETRY='off'
GOTELEMETRYDIR=''
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/nix/store/kh43nhaz1qcpwws2xq805lrmwpmn9i3k-go-1.26.1/share/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.26.1'
GOWORK=''
PKG_CONFIG='pkg-config'

What did you do?

package main

import "fmt"

func main() {
	var i int = 42
	fmt.Printf("%q\n", i)
}

https://go.dev/play/p/19ECZqyEHs_A

What did you see happen?

prog.go:7:14: fmt.Printf format %q has arg i of wrong type int

Go vet failed.

'*'

Program exited.

What did you expect to see?

The fmt docs state that the %q verb can be used to print integers:
%q a single-quoted character literal safely escaped with Go syntax.:
https://pkg.go.dev/fmt#:~:text=Integer%3A

Image

As of Go 1.26, this however no longer passes go vet:

$ go vet prog.go
prog.go:7:14: fmt.Printf format %q has arg i of wrong type int

The docs on fmt should reflect that.

That vet error was introduced as part of #72850.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationIssues describing a change to documentation.NeedsFixThe path to resolution is known, but the work has not been done.help wanted

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions