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: an unknown verb on a struct should just print each field with that verb, same as for arrays #2889

Sajmani opened this issue Feb 6, 2012 · 1 comment


Copy link

@Sajmani Sajmani commented Feb 6, 2012

What steps will reproduce the problem?
1. go run fmt.go (attached)

What is the expected output? What do you see instead?

We have:
type foo struct {
    n int
    t time.Time
    p *foo

We have this time:

--- t = time.Now() ---
%#v == time.Time{sec:63464136979, nsec:565853000, loc:(*time.Location)(0x4e8620)}
%s == Mon Feb  6 09:56:19 -0500 EST 2012
%v == Mon Feb  6 09:56:19 -0500 EST 2012
%+v == Mon Feb  6 09:56:19 -0500 EST 2012

And this instance of foo containing that time:

--- y = &foo{t: t} ---
%#v == &{n:0, t:time.Time{sec:63464136979, nsec:565853000,
loc:(*time.Location)(0x4e8620)}, p:(*}
%s == &{%!s(int=0) {%!s(int64=63464136979) %!s(int32=565853000) 0x4e8620}
%v == &{0 {63464136979 565853000 0x4e8620} <nil>}
%+v == &{n:0 t:{sec:63464136979 nsec:565853000 loc:0x4e8620} p:<nil>}

None of these verbs call time's String method, whereas %s, %v, and %+v do when t is
passed directly.

This all works fine if the struct type defines its own String method that prints the
time member with %s; see type bar in the attached program.


  1. fmt.go (946 bytes)
Copy link

@rsc rsc commented Jun 26, 2012

Comment 1:

This happens because the fields are unexported. Reflect won't let you call methods on a
value obtained by use of an unexported field, so fmt cannot call these String methods.

Status changed to Unfortunate.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.