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: Why is "fmt" using the error interface when used with %v and %+v? #23551

Closed
cvigo opened this issue Jan 25, 2018 · 2 comments
Closed

fmt: Why is "fmt" using the error interface when used with %v and %+v? #23551

cvigo opened this issue Jan 25, 2018 · 2 comments

Comments

@cvigo
Copy link

cvigo commented Jan 25, 2018

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

go1.9.2

Does this issue reproduce with the latest release?

yes

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

darwin/amd64

What did you do?

https://play.golang.org/p/LiUkLY1e9c3

What did you expect to see?

&{The Message 1 [a b c]}
&{Message:The Message Code:1 Details:[a b c]}
&main.MyError{Message:"The Message", Code:1, Details:[]string{"a", "b", "c"}}
&{The Message %!s(int=1) [a b c]}

What did you see instead?

This is an error
This is an error
&main.MyError{Message:"The Message", Code:1, Details:[]string{"a", "b", "c"}}
This is an error
@ianlancetaylor
Copy link
Contributor

This is working as documented. Quoting https://golang.org/pkg/fmt/ :

Except when printed using the verbs %T and %p, special formatting considerations apply for operands that implement certain interfaces. In order of application:
...
4. If an operand implements the error interface, the Error method will be invoked to convert the object to a string, which will then be formatted as required by the verb (if any).

Closing.

@cvigo
Copy link
Author

cvigo commented Jan 25, 2018

I know is working as documented. Just wanted to point out that this makes sense for %s but at least not for %+v or %v (to my understanding)

Thanks anyway

@mikioh mikioh changed the title Why is "fmt" using the error interface when used with %v and %+v? fmt: Why is "fmt" using the error interface when used with %v and %+v? Jan 26, 2018
@golang golang locked and limited conversation to collaborators Jan 26, 2019
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

3 participants