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

builtin: Unable to print []byte alias function receiver #7081

Closed
gopherbot opened this issue Jan 8, 2014 · 1 comment
Closed

builtin: Unable to print []byte alias function receiver #7081

gopherbot opened this issue Jan 8, 2014 · 1 comment

Comments

@gopherbot
Copy link

@gopherbot gopherbot commented Jan 8, 2014

by charlieandrews.cwa:

An alias of []byte cannot be printed with fmt.Printf("%x") when the alias is
the receiver of the function. This is demonstrated here:
http://play.golang.org/p/0sTP27XZa4

In go play, I get '[process took too long]', but on my local machine, the program
containing this code compiles fine, but I get a stack overflow error when running it. 

If I cast the alias back to []byte it prints fine: http://play.golang.org/p/WOBMERNSV1
@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Jan 8, 2014

Comment 1:

This is documented and intended behaviour.  Quoting http://golang.org/pkg/fmt:
If the format (which is implicitly %v for Println etc.) is valid for a string (%s %q %v
%x %X), the following two rules also apply:
1. If an operand implements the error interface, the Error method will be used to
convert the object to a string, which will then be formatted as required by the verb (if
any).
2. If an operand implements method String() string, that method will be used to convert
the object to a string, which will then be formatted as required by the verb (if any).
To avoid recursion in cases such as
type X string
func (x X) String() string { return Sprintf("<%s>", x) }
convert the value before recurring:
func (x X) String() string { return Sprintf("<%s>", string(x)) }

Status changed to WorkingAsIntended.

@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
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants