doukong1897 2014-04-21 17:56
浏览 299
已采纳

在GoLang中为uint64分配类型uintptr

I'm trying to assign the value found in a variable of type uintptr to a uint64 variable in Go. Using

myVar = valFromSystem

gives me

cannot use valFromSystem (type uintptr) as type uint64 in assignment

And trying

myVar = *valFromSystem

gives me

invalid indirect of valFromSystem (type uintptr)

Is there a way to pull the value pointed to by valFromSystem to assign to myVar?

  • 写回答

1条回答 默认 最新

  • doushi1974 2014-04-21 18:01
    关注

    First, cast valFromSystem into an unsafe.Pointer. An unsafe.Pointer can be casted into any pointer type. Next, cast the unsafe.Pointer into a pointer to whatever type of data valFromSystem points to, e.g. an uint64.

    ptrFromSystem = (*uint64)(unsafe.Pointer(valFromSystem))
    

    If you just want to get the value of the pointer (without dereferencing it), you can use a direct cast:

    uint64FromSystem = uint64(valFromSystem)
    

    Though remember that you should use the type uintptr when using pointers as integers.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失