Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
24ee7f79
Commit
24ee7f79
authored
Feb 25, 2010
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
%q in fmt: if the object is a Stringer, use String() to get the value to quote.
R=rsc CC=golang-dev
https://golang.org/cl/224051
parent
d5248c4a
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
11 additions
and
1 deletion
+11
-1
src/pkg/fmt/fmt_test.go
src/pkg/fmt/fmt_test.go
+3
-0
src/pkg/fmt/print.go
src/pkg/fmt/print.go
+7
-0
src/pkg/fmt/stringer_test.go
src/pkg/fmt/stringer_test.go
+1
-1
No files found.
src/pkg/fmt/fmt_test.go
View file @
24ee7f79
...
...
@@ -217,6 +217,9 @@ var fmttests = []fmtTest{
fmtTest
{
"%+v"
,
B
{
1
,
2
},
`{i:<1> j:2}`
},
fmtTest
{
"%+v"
,
C
{
1
,
B
{
2
,
3
}},
`{i:1 B:{i:<2> j:3}}`
},
// q on Stringable items
fmtTest
{
"%q"
,
I
(
23
),
`"<23>"`
},
// %p on non-pointers
fmtTest
{
"%p"
,
make
(
chan
int
),
"PTR"
},
fmtTest
{
"%p"
,
make
(
map
[
int
]
int
),
"PTR"
},
...
...
src/pkg/fmt/print.go
View file @
24ee7f79
...
...
@@ -912,6 +912,13 @@ func (p *pp) doprintf(format string, a []interface{}) {
goto
badtype
}
case
'q'
:
if
field
!=
nil
{
// if object implements String, use the result.
if
stringer
,
ok
:=
field
.
(
Stringer
);
ok
{
p
.
fmt
.
fmt_q
(
stringer
.
String
())
break
}
}
if
v
,
ok
:=
getString
(
field
);
ok
{
p
.
fmt
.
fmt_q
(
v
)
}
else
{
...
...
src/pkg/fmt/stringer_test.go
View file @
24ee7f79
...
...
@@ -41,7 +41,7 @@ func (v TF) String() string { return Sprintf("F: %f", v) }
func
(
v
TF32
)
String
()
string
{
return
Sprintf
(
"F32: %f"
,
v
)
}
func
(
v
TF64
)
String
()
string
{
return
Sprintf
(
"F64: %f"
,
v
)
}
func
(
v
TB
)
String
()
string
{
return
Sprintf
(
"B: %t"
,
v
)
}
func
(
v
TS
)
String
()
string
{
return
Sprintf
(
"S: %q"
,
v
)
}
func
(
v
TS
)
String
()
string
{
return
Sprintf
(
"S: %q"
,
string
(
v
)
)
}
func
check
(
t
*
testing
.
T
,
got
,
want
string
)
{
if
got
!=
want
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment