mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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:
parent
66bedf8221
commit
00d64c7239
2 changed files with 50 additions and 0 deletions
|
|
@ -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])
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue