在Google Go中将重命名类型转换

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

Cast from renamed type in google go

问题

我正在尝试编写适用的方法将appengine/datastore.Time类型转换为字符串。
这个类型被声明为type Time int64
但是当我尝试将它用作int64值时:

localTime := time.SecondsToLocalTime(t/1000)

我收到错误消息

> 无法将t / 1000(类型为datastore.Time)作为int64类型传递给函数参数

从int64赋值给Time类型的变量是成功的,但是如何将其强制转换回来呢?

英文:

I'm trying to write suitable method of converting appengine/datastore.Time type to string.
This type is declared as type Time int64
But when i'm trying to use it as int64 value:

localTime := time.SecondsToLocalTime(t/1000)

I'm receiving error message

> cannot use t / 1000 (type
> datastore.Time) as type int64 in
> function argument

Assignment from int64 to Time typed variable is successfull, but how can i cast it back?

答案1

得分: 4

像这样做

time.SecondsToLocalTime(int64(t)/1000)

转换的文档中查看更多信息

英文:

do it like

time.SecondsToLocalTime(int64(t)/1000)

See more in the documentation about Conversions

huangapple
  • 本文由 发表于 2011年6月2日 02:59:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/6206287.html
匿名

发表评论

匿名网友

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

确定