-
Notifications
You must be signed in to change notification settings - Fork 7
/
example_test.go
39 lines (33 loc) · 790 Bytes
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package xunsafe_test
import (
"fmt"
"github.com/viant/xunsafe"
"reflect"
)
func Example_FastReflection() {
type Foo struct {
ID int
Name string
}
fooType := reflect.TypeOf(Foo{})
fooID := xunsafe.FieldByName(fooType, "ID")
fooName := xunsafe.FieldByName(fooType, "Name")
var foos = make([]Foo, 100)
for i := range foos {
fooAddr := xunsafe.EnsurePointer(&foos[i])
fooID.SetInt(fooAddr, i)
fooName.SetString(fooAddr, fmt.Sprintf("name %d", i))
}
}
func ExampleAddr() {
type Foo struct {
ID int
Name string
}
fooType := reflect.TypeOf(Foo{})
fooID := xunsafe.FieldByName(fooType, "ID")
foo := &Foo{ID: 101, Name: "name 101"}
fooAddr := xunsafe.EnsurePointer(foo)
*(fooID.Addr(fooAddr).(*int)) = 201
fmt.Printf("foo.ID: %v\n", foo.ID) //prints 201
}