Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.Sign up
GitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
fmt: Infinite loop for specific integer verbs in String method. #8374
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
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.