Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
b0d62676
Commit
b0d62676
authored
Dec 22, 2008
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
print array (not just *array) using %v
TBR=rsc DELTA=34 (33 added, 0 deleted, 1 changed) OCL=21718 CL=21718
parent
eb322286
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
34 additions
and
1 deletion
+34
-1
fmt_test.go
src/lib/fmt/fmt_test.go
+14
-0
print.go
src/lib/fmt/print.go
+20
-1
No files found.
src/lib/fmt/fmt_test.go
View file @
b0d62676
...
...
@@ -232,3 +232,17 @@ export func TestStructPrinter(t *testing.T) {
}
}
}
export
func
TestArrayPrinter
(
t
*
testing
.
T
)
{
a
:=
[]
int
{
1
,
2
,
3
,
4
,
5
};
want
:=
"[1 2 3 4 5]"
;
out
:=
fmt
.
sprintf
(
"%v"
,
a
);
if
out
!=
want
{
t
.
Errorf
(
"sprintf(%%v, array) = %q, want %q"
,
out
,
want
);
}
want
=
"&"
+
want
;
out
=
fmt
.
sprintf
(
"%v"
,
&
a
);
if
out
!=
want
{
t
.
Errorf
(
"sprintf(%%v, &array) = %q, want %q"
,
out
,
want
);
}
}
src/lib/fmt/print.go
View file @
b0d62676
...
...
@@ -305,6 +305,14 @@ func getArrayPtr(v reflect.Value) (val reflect.ArrayValue, ok bool) {
return
nil
,
false
;
}
func
getArray
(
v
reflect
.
Value
)
(
val
reflect
.
ArrayValue
,
ok
bool
)
{
switch
v
.
Kind
()
{
case
reflect
.
ArrayKind
:
return
v
.
(
reflect
.
ArrayValue
),
true
;
}
return
nil
,
false
;
}
// Convert ASCII to integer. n is 0 (and got is false) if no number present.
func
parsenum
(
s
string
,
start
,
end
int
)
(
n
int
,
got
bool
,
newi
int
)
{
...
...
@@ -365,7 +373,7 @@ func (p *P) printField(field reflect.Value) (was_string bool) {
if
v
,
ok
:=
getPtr
(
field
);
v
==
0
{
s
=
"<nil>"
}
else
{
// pointer to array?
// pointer to array?
(TODO(r): holdover; delete?)
if
a
,
ok
:=
getArrayPtr
(
field
);
ok
{
p
.
addstr
(
"&["
);
for
i
:=
0
;
i
<
a
.
Len
();
i
++
{
...
...
@@ -380,6 +388,17 @@ func (p *P) printField(field reflect.Value) (was_string bool) {
s
=
p
.
fmt
.
uX64
(
uint64
(
v
))
.
str
();
}
}
case
reflect
.
ArrayKind
:
if
a
,
ok
:=
getArray
(
field
);
ok
{
p
.
addstr
(
"["
);
for
i
:=
0
;
i
<
a
.
Len
();
i
++
{
if
i
>
0
{
p
.
addstr
(
" "
);
}
p
.
printField
(
a
.
Elem
(
i
));
}
p
.
addstr
(
"]"
);
}
case
reflect
.
StructKind
:
p
.
add
(
'{'
);
v
:=
field
.
(
reflect
.
StructValue
);
...
...
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