summaryrefslogtreecommitdiff
path: root/src/lib/fmt
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2009-04-06 21:28:04 -0700
committerRuss Cox <rsc@golang.org>2009-04-06 21:28:04 -0700
commitf460c5c233a8ed87c2b6e5efc7202794e84022f1 (patch)
tree96684cc7d61274ff46cca10667e8d30a6af67a2c /src/lib/fmt
parent71a88a79ac75ccaca1986af7d3616563cbb2f74f (diff)
downloadgolang-f460c5c233a8ed87c2b6e5efc7202794e84022f1.tar.gz
add method Value() Value to InterfaceValue.
use Value() in print to print underlying value from interface. before: package main import "fmt" func main() { x := []interface{} {1, "hello", 2.5}; fmt.Println(x[0], x[1], x[2], x); } 1 hello 2.5 [<non-nil interface> <non-nil interface> <non-nil interface>] after: 1 hello 2.5 [1 hello 2.5] R=r DELTA=44 (22 added, 16 deleted, 6 changed) OCL=27139 CL=27141
Diffstat (limited to 'src/lib/fmt')
-rw-r--r--src/lib/fmt/fmt_test.go22
-rw-r--r--src/lib/fmt/print.go7
2 files changed, 7 insertions, 22 deletions
diff --git a/src/lib/fmt/fmt_test.go b/src/lib/fmt/fmt_test.go
index 34acab08b..e4158624b 100644
--- a/src/lib/fmt/fmt_test.go
+++ b/src/lib/fmt/fmt_test.go
@@ -29,7 +29,7 @@ type fmtTest struct {
const b32 uint32 = 1<<32 - 1
const b64 uint64 = 1<<64 - 1
var array = []int{1, 2, 3, 4, 5}
-
+var iarray = []interface{}{1, "hello", 2.5, nil}
var fmttests = []fmtTest{
// basic string
@@ -80,10 +80,10 @@ var fmttests = []fmtTest{
fmtTest{ "% d", -12345, "-12345" },
// arrays
- // TODO: when arrays work in interfaces, enable this line
- // and delete the TestArrayPrinter routine below
- // fmtTest{ "%v", array, "[1 2 3 4 5]" },
+ fmtTest{ "%v", array, "[1 2 3 4 5]" },
+ fmtTest{ "%v", iarray, "[1 hello 2.5 <nil>]" },
fmtTest{ "%v", &array, "&[1 2 3 4 5]" },
+ fmtTest{ "%v", &iarray, "&[1 hello 2.5 <nil>]" },
// old test/fmt_test.go
fmtTest{ "%d", 1234, "1234" },
@@ -240,17 +240,3 @@ func TestStructPrinter(t *testing.T) {
}
}
}
-
-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);
- }
-}
diff --git a/src/lib/fmt/print.go b/src/lib/fmt/print.go
index ca5bec934..5fd230f2c 100644
--- a/src/lib/fmt/print.go
+++ b/src/lib/fmt/print.go
@@ -451,12 +451,11 @@ func (p *pp) printField(field reflect.Value) (was_string bool) {
}
p.add('}');
case reflect.InterfaceKind:
- inter := field.(reflect.InterfaceValue).Get();
- if inter == nil {
+ value := field.(reflect.InterfaceValue).Value();
+ if value == nil {
s = "<nil>"
} else {
- // should never happen since a non-nil interface always has a type
- s = "<non-nil interface>";
+ return p.printField(value);
}
default:
s = "?" + field.Type().String() + "?";