reflect: add Convert() for string -> []byte and []byte -> string

This commit is contained in:
Damian Gryski
2023-03-10 23:00:12 -08:00
committed by Ayke
parent 72c7adf94a
commit 0b6bb12e9e
3 changed files with 82 additions and 0 deletions
+8
View File
@@ -579,6 +579,14 @@ func TestConvert(t *testing.T) {
if c.Type().Kind() != Uint8 || c.Uint() != 3 {
t.Errorf("Conver(uint64 -> byte failed: kind=%v, value=%d", c.Type().Kind().String(), c.Uint())
}
v = ValueOf("hello")
c = v.Convert(TypeOf([]byte("")))
if c.Type().Kind() != Slice || c.Type().Elem().Kind() != Uint8 && c.Len() != 5 && string(c.Bytes()) != "hello" {
t.Errorf("Conver(string -> []byte")
}
}
func equal[T comparable](a, b []T) bool {