Golang 利用unsafe.Pointer进行指针操作

Jackey Golang 2,008 次浏览 , 没有评论
func main() {
    data := []int{1, 2, 3}
    for i := 0; i < len(data); i++ {
        ptr := unsafe.Pointer(uintptr(unsafe.Pointer(&data[0])) + uintptr(i)*unsafe.Sizeof(data[0]))
        fmt.Printf("%d ", *(*int)(unsafe.Pointer(ptr)))
    }
    fmt.Printf("\n")
    str := []string{"a", "b", "c"}
    for i := 0; i < len(data); i++ {
        ptr := unsafe.Pointer(uintptr(unsafe.Pointer(&str[0])) + uintptr(i)*unsafe.Sizeof(str[0]))
        fmt.Printf("%s ", *(*string)(unsafe.Pointer(ptr)))
    }
    fmt.Printf("\n")

    // 利用指针修改下标为1的值
    ptr := unsafe.Pointer(uintptr(unsafe.Pointer(&str[0])) + uintptr(1)*unsafe.Sizeof(str[0]))
    *(*string)(unsafe.Pointer(ptr)) = "d"
    fmt.Println(str)

    // 利用指针修改下标为0的值
    ptr = unsafe.Pointer(uintptr(unsafe.Pointer(&data[0])) + uintptr(0)*unsafe.Sizeof(data[0]))
    *(*int)(unsafe.Pointer(ptr)) = 5
    fmt.Println(data)
}

 

发表回复

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

Go