在部署的App Engine应用中,用户的ID是否可以超过64位?

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

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 strings, so treat them like that: strings. 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 strings.

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的范围是-21474836482147483647,这对于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.

huangapple
  • 本文由 发表于 2015年11月29日 09:26:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/33978419.html
匿名

发表评论

匿名网友

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

确定