You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ntool-biz/zero/statusz/handler_test.go

73 lines
1.2 KiB
Go

1 year ago
package statusz
import (
"encoding/json"
"fmt"
"git.noahlan.cn/noahlan/ntool-biz/core/nstatus"
"reflect"
"testing"
"unsafe"
)
type Dat struct {
Nickname string `json:"nickname"`
}
func TestA(t *testing.T) {
type A struct {
Code int64 `json:"code"` // 8
Msg string `json:"msg"` // 16
Data any `json:"data"`
}
type Response struct {
A
Data Dat
}
r := new(Response)
r.Code = 123
r.Msg = "222"
r.Data = Dat{Nickname: "2333"}
marshal, err := json.Marshal(r)
if err != nil {
return
}
fmt.Println(string(marshal))
fmt.Println(r)
fmt.Printf("r:%p code:%p data:%p\n", r, &r.Code, &r.Data)
a := (*nstatus.Result)(unsafe.Pointer(r))
trans(r)
fmt.Println(a)
fmt.Printf("a:%p code:%p data:%p\n", a, &a.Code, &a.Data)
}
func trans(resp any) {
fmt.Println()
type ApiResult struct {
Code int64 `json:"code"` // 8
Msg string `json:"msg"` // 16
Data any `json:"data"`
}
//ptr :=
//u := (*[2]uintptr)(unsafe.Pointer(&resp))[1]
u := (*ApiResult)(reflect.ValueOf(resp).UnsafePointer())
dataPtr := unsafe.Add(reflect.ValueOf(resp).UnsafePointer(), 40)
fmt.Println(dataPtr)
u.Data = unsafe.Alignof(dataPtr)
fmt.Println(u)
fmt.Printf("u:%p code:%p data:%p\n", u, &u.Code, &u.Data)
fmt.Println()
}