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: documentation on Printf * modifier is misleading #22275

Closed
dpinela opened this issue Oct 14, 2017 · 5 comments
Closed

fmt: documentation on Printf * modifier is misleading #22275

dpinela opened this issue Oct 14, 2017 · 5 comments

Comments

@dpinela
Copy link
Contributor

dpinela commented Oct 14, 2017

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

go version go1.9 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

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

GOARCH="amd64"
GOBIN=""
GOEXE=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/dpinela/dev/go"
GORACE=""
GOROOT="/usr/local/Cellar/go/1.9/libexec"
GOTOOLDIR="/usr/local/Cellar/go/1.9/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/41/2xpv_r1j5n5bnflwb7s1hv580000gp/T/go-build477584587=/tmp/go-build -gno-record-gcc-switches -fno-common"
CXX="clang++"
CGO_ENABLED="1"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"

What did you do?

Run this program: https://play.golang.org/p/uBOhH86MEv

What did you expect to see?

The program prints:

$%!(BADWIDTH)%!g(int=8)
$00001.68

What did you see instead?

It prints the following instead:

$00001.68
$%!(BADWIDTH)%!g(int=8)

The documentation says this about the behaviour of the * modifier (emphasis mine):

Width and precision are measured in units of Unicode code points, that is, runes. (This differs from C's printf where the units are always measured in bytes.) Either or both of the flags may be replaced with the character '*', causing their values to be obtained from the next operand, which must be of type int.

I was therefore expecting that Printf would get the width from the argument following the one to be formatted, rather than the preceding one. I think the wording could be clarified, such as by specifying "causing their values to be obtained from the next operand, before obtaining the value to be formatted", and/or by adding an example.

@odeke-em
Copy link
Member

/cc @robpike

@robpike
Copy link
Contributor

robpike commented Oct 15, 2017

The arguments are read left to right. That's always the case.

The problem is with the word 'next', which is so vexing in conversation that people construct circumlocutions for clarity, as in "The next Tuesday we will experience." It seems unlikely we need to invent verbiage for this.

@gopherbot
Copy link

Change https://golang.org/cl/71010 mentions this issue: fmt: clarify wording of * flag

@odeke-em
Copy link
Member

@dpinela, @robpike has mailed out a CL. Just in case you aren't able to get onto Gerrit
he has amended the comment to now be:

obtained from the next unread operand (preceding the one to format),
	which must be of type int.

and that looks quite similar to your suggestion.

@dpinela
Copy link
Contributor Author

dpinela commented Oct 15, 2017

That looks better than my suggestion, even. Thanks.

@golang golang locked and limited conversation to collaborators Oct 15, 2018
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