从golang中的fixed64 protobuf字段中读取int64。

huangapple go评论78阅读模式
英文:

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

huangapple
  • 本文由 发表于 2016年2月2日 06:31:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/35141669.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定