type animal struct {
    name string
    kind string
}

func main() {
    lion := animal{name: "lion", kind: "mammal"}
    //第一个成员
    name := (*string)(unsafe.Pointer(&lion))
    *name = "tiger"
    fmt.Println(lion)

    name1 := (*string)(unsafe.Pointer(uintptr(unsafe.Pointer(&lion)) + unsafe.Offsetof(lion.name)))
    *name1 = "tiger1"
    fmt.Println(lion)

    kind := (*string)(unsafe.Pointer(uintptr(unsafe.Pointer(&lion)) + unsafe.Offsetof(lion.kind)))
    *kind = "cat"
    fmt.Println(lion)
}

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code