You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 == &main.foo{n:0, t:time.Time{sec:63464136979, nsec:565853000,
loc:(*time.Location)(0x4e8620)}, p:(*main.foo)(nil)}
%s == &{%!s(int=0) {%!s(int64=63464136979) %!s(int32=565853000) 0x4e8620}
<nil>}
%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.
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.
Attachments:
The text was updated successfully, but these errors were encountered: