英文:
read int64 from fixed64 protobuf field in golang
问题
我有一个在.proto文件中类型为fixed64
的字段。
我想将其作为int64字段进行读取:
score := int64(pb_obj.Score)
当我尝试编译上述代码时,我收到错误消息无法将pb_obj.Score(类型为*uint64)转换为int64类型
。我还尝试将uint64转换为int64,得到了几乎相同的错误消息。
英文:
I have a field that is of type fixed64
in a .proto file.
I want to read it as an int64 field:
score := int64(pb_obj.Score)
When I try to compile the line agove I get the error message cannot convert pb_obj.Score (type *uint64) to type int64
. I tried converting the a uint64 as well, and got an almost identical message.
答案1
得分: 3
pb_obj.Score
的类型似乎是*uint64
(指向uint64
的指针),而不是uint64
。你只需要访问指针引用的值:
score := int64(*pb_obj.Score)
(将*
前缀视为区别)
英文:
pb_obj.Score
's type seems to be *uint64
(pointer to uint64
), not uint64
. You just need to access to the value the pointer is referencing:
score := int64(*pb_obj.Score)
(See the *
prefix as the difference)
答案2
得分: 2
根据编译错误,你正在使用一个uint64指针而不是uint64值。你可以通过使用*运算符直接引用该值来获得你想要的结果。我从未使用过protobuf,所以可能不准确,但这应该能帮助你解决问题。这里有一个很好的参考资料golang指针。
英文:
Based on the compile error you're working with a uint64 pointer and not a uint64 value. You may get what you want by referencing the value directly using the * operator. I've never worked with protobuf, so I could be off but that should get you moving. Here's a nice reference that may help golang pointers
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论