reflect: add Value.Bytes, Value.SetBytes methods

This allows code that wants to handle
[]byte separately to get at the actual slice
instead of just at individual bytes.
It seems to come up often enough.

R=r
CC=golang-dev
https://golang.org/cl/4942051
This commit is contained in:
Russ Cox 2011-08-23 22:50:08 -04:00
parent 66bedf8221
commit 00d64c7239
2 changed files with 50 additions and 0 deletions

View file

@ -1562,3 +1562,28 @@ func TestTagGet(t *testing.T) {
}
}
}
func TestBytes(t *testing.T) {
type B []byte
x := B{1, 2, 3, 4}
y := ValueOf(x).Bytes()
if !bytes.Equal(x, y) {
t.Fatalf("ValueOf(%v).Bytes() = %v", x, y)
}
if &x[0] != &y[0] {
t.Errorf("ValueOf(%p).Bytes() = %p", &x[0], &y[0])
}
}
func TestSetBytes(t *testing.T) {
type B []byte
var x B
y := []byte{1, 2, 3, 4}
ValueOf(&x).Elem().SetBytes(y)
if !bytes.Equal(x, y) {
t.Fatalf("ValueOf(%v).Bytes() = %v", x, y)
}
if &x[0] != &y[0] {
t.Errorf("ValueOf(%p).Bytes() = %p", &x[0], &y[0])
}
}