github.com/bingoohuang/gg@v0.0.0-20240325092523-45da7dee9335/pkg/ginx/hlog/hack_test.go (about) 1 package hlog_test 2 3 import ( 4 "fmt" 5 "testing" 6 "unsafe" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 type A interface { 12 Do() string 13 } 14 15 type obj1 struct { 16 age int 17 } 18 19 func (a *obj1) Do() string { return fmt.Sprintf("%d", a.age) } 20 21 type obj2 struct { 22 name string 23 } 24 25 func (a *obj2) Do() string { return a.name } 26 27 type obj3 struct { 28 A 29 j int64 // nolint:structcheck,unused 30 } 31 32 func TestReplaceResponseWriter(t *testing.T) { 33 var o1 A = &obj3{A: &obj1{age: 100}} 34 assert.Equal(t, "100", o1.Do()) 35 36 no1 := unsafe.Pointer(&o1) 37 no1a := (*A)(no1) 38 *no1a = &obj2{name: "bingoo"} 39 o1.Do() 40 41 assert.Equal(t, "bingoo", o1.Do()) 42 43 type Num struct { 44 i string 45 j int64 46 } 47 48 n := Num{i: "EDDYCJY", j: 1} 49 nPointer := unsafe.Pointer(&n) 50 51 niPointer := (*string)(nPointer) 52 *niPointer = "煎鱼" 53 54 njPointer := (*int64)(unsafe.Pointer(uintptr(nPointer) + unsafe.Offsetof(n.j))) 55 *njPointer = 2 56 57 assert.Equal(t, "n.i: 煎鱼, n.j: 2", fmt.Sprintf("n.i: %s, n.j: %d", n.i, n.j)) 58 }