英文:
In deployed appengine apps, can a User's ID be longer than 64 bits?
问题
我正在使用Go语言开发一个App Engine应用程序,当我尝试获取一个App Engine的User
的ID并将其转换为64位整数时,使用了以下代码:
id, err := strconv.ParseInt(user.ID, 10, 64)
但是我遇到了以下错误:
ERROR: error executing inner: strconv.ParseInt: parsing "185804764220139124118": value out of range
这个错误在部署的代码中的App Engine ID上也会发生吗?
英文:
I am working on an appengine app in Go and when I tried to get an appengine User
's ID and convert it to a 64-bit int with this code:
id, err := strconv.ParseInt(user.ID, 10, 64)
I got this error:
ERROR: error executing inner: strconv.ParseInt: parsing "185804764220139124118": value out of range
Does this error occur on appengine IDs in deployed code as well?
答案1
得分: 4
User
类型的 ID
字段被定义为 string
类型。仅仅因为它包含数字,并不能保证它适合于 int64
类型。
Go AppengineSDK 使用适合于 int64
的值,但在生产环境中并非如此。它们通常比 int64
的最大值要长。它们以 string
的形式传递给您,所以将它们视为 string
处理。没有任何强制要求您将它们转换为数字。我不知道您从哪里得到将它们转换为数字的想法,但请不要这样做。它们是 string
类型的。
注意:User.ID
不应与 Key.IntID()
混淆,后者被定义为 int64
类型。
英文:
The ID
field of the User
type is defined with type string
. Just because it contains digits, there is no guarantee that it fits into an int64
.
The Go AppengineSDK uses values that fit into int64
, but this is not true in production environment. They are usually longer than the max value of int64
. They are handed to you as string
s, so treat them like that: string
s. Nothing forces you to convert them to a number. I don't know where you got the idea to convert them to numbers, but don't do that. They are string
s.
Note: User.ID
is not to be mistaken with Key.IntID()
which is defined to be of type int64
.
答案2
得分: -1
很可能是的。根据应用引擎文档的说明:
> 默认策略会生成一个随机序列的未使用ID,这些ID大致上是均匀分布的。每个ID最多可以有16位十进制数字。
根据Go规范,int32的范围是-2147483648
到2147483647
,这对于16位十进制数来说是不够的,但int64
则足够。
英文:
Most probably yes. Reading app engine documentation, it is written that:
> The default policy generates a random sequence of unused IDs that are
> approximately uniformly distributed. Each ID can be up to 16 decimal
> digits long.
From Go specs int32 is -2147483648 through 2147483647.
which is not enough for 16 decimals, but int64
is enough.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论