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: Infinite loop for specific integer verbs in String method. #8374

Closed
mewmew opened this issue Jul 16, 2014 · 3 comments
Closed

fmt: Infinite loop for specific integer verbs in String method. #8374

mewmew opened this issue Jul 16, 2014 · 3 comments
Milestone

Comments

@mewmew
Copy link
Contributor

mewmew commented Jul 16, 2014

What steps reproduce the problem?
If possible, include a link to a program on play.golang.org.

ref: http://play.golang.org/p/XEZS8bg0HJ

Given a type T with an underlying integer type, define a String method on T and use
fmt.Sprintf with one of the integer verbs to returns a string.

What happened?

It works just fine for the following integer verbs:
   'b', 'c', 'd', 'o' and 'U'
but it doesn't work for any of the following integer verbs:
   'q', 'x' and 'X'

This is because the verbs 'q', 'x' and 'X' are also valid string verbs; which have
special treatment.
Ref: http://golang.org/src/pkg/fmt/print.go#L678


What should have happened instead?
It should work for all integer verbs, if the underlying type is an integer and not a
string.

Please provide any additional information below.
go version devel +9562b65a3c51 Wed Jul 16 20:48:57 2014 +1000 linux/amd64
@ianlancetaylor
Copy link
Contributor

Comment 1:

I'm not sure what the correct behaviour is here.

Labels changed: added repo-main, release-go1.4.

@dominikh
Copy link
Member

Comment 2:

This is already a somewhat regular occurrence when having a type T string that
implements the error interface and needs to print itself, in which case the accepted
solution is to add a type conversion to the underlying type.
So in this case return fmt.Sprintf("%q", int(t))
Types in Go convey method sets -- If you don't want your type to have a certain method
set (in this case one that includes the String method), convert it to a different type
that has the same underlying type but a different method set.

@robpike
Copy link
Contributor

robpike commented Jul 16, 2014

Comment 3:

Working as intended, and unfortunate.

Status changed to WorkingAsIntended.

@rsc rsc added this to the Go1.4 milestone Apr 14, 2015
@rsc rsc removed the release-go1.4 label Apr 14, 2015
@golang golang locked and limited conversation to collaborators Jun 25, 2016
This issue was closed.
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

6 participants