GoLang – How to initialize value to *time.Time

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

GoLang - How to initialize value to *time.Time

问题

我可以帮你翻译这段内容。以下是翻译好的内容:

我在某个device.go文件中定义了一个结构体。

type Payload struct {
    ...
	Timestamp *time.Time  `json:"timestamp,omitempty"`
    ...
}

现在我想在另一个文件中初始化这个结构体,我尝试了以下代码:

payload := &device.Payload{
    ....
	Timestamp: time.Now(), // 报错
    ... // 其他初始化工作正常
}

cannot use time.Now() (value of type time.Time) as *time.Time value in
struct literal

我想将变量初始化为当前的UTC时间。我对Go语言还不太熟悉。我该如何做到这一点?

英文:

I have a struct defined in some device.go file

type Payload struct {
    ...
	Timestamp *time.Time  `json:"timestamp,omitempty"`
    ...
}

Now I want to Initialize this struct in some other file and I'm trying this:

payload:= &device.Payload{
    ....
	Timestamp: time.Now(), // throws error
    ... //other initializations work fine
}

> cannot use time.Now() (value of type time.Time) as *time.Time value in
> struct literal

I want to initialize the variable as the current UTC time. I'm new to golang. How do I do that?

答案1

得分: 7

time.Now() 返回的是 time.Time 类型的值,而不是 *time.Time

请注意,你也不能获取函数调用的返回值的地址,也不能使用 &time.Now()。具体详情请参考 https://stackoverflow.com/questions/30744965/how-to-get-the-pointer-of-return-value-from-function-call/30751102#30751102

你可以使用一个类型为 time.Time 的变量并获取其地址:

var t = time.Now()

payload:= &device.Payload{
    Timestamp: &t,
}

另一个“常用”的选项是创建一个辅助函数来返回 time.Time 值的地址(你可以直接传递函数调用的返回值):

func timePtr(t time.Time) *time.Time {
    return &t
}

payload:= &device.Payload{
    Timestamp: timePtr(time.Now()),
}

有关详情和其他选项,请参见 How do I do a literal *int64 in Go?

我假设你想要一个指针,因为你想要允许“缺失时间”的选项,这对于 encoding/json 包来说是必要的(详情请参考:https://stackoverflow.com/questions/32643815/json-omitempty-with-time-time-field/32646035#32646035)。请注意,在其他情况下,自动转换为指针可能是不必要的:你可以使用 time.Time 的零值(一个结构体)来表示“缺失时间”的状态。要轻松判断一个 time.Time 是否为其零值,你可以使用 Time.IsZero()。相关内容请参考:https://stackoverflow.com/questions/52216908/idiomatic-way-to-represent-optional-time-time-in-a-struct/52217049#52217049

英文:

time.Now() returns a value of type time.Time, which is not *time.Time.

Note that you also cannot take the address of the return values of function calls, you can't do &time.Now() either. For details, see https://stackoverflow.com/questions/30744965/how-to-get-the-pointer-of-return-value-from-function-call/30751102#30751102

What you may do is use a variable of type time.Time and take its address:

var t = time.Now()

payload:= &device.Payload{
    Timestamp: &t,
}

Another "popular" option is to create a helper function to return the address of a time.Time value (to which you may pass the return value of a function call directly):

func timePtr(t time.Time) *time.Time {
    return &t
}

payload:= &device.Payload{
    Timestamp: timePtr(time.Now()),
}

For details and other options, see How do I do a literal *int64 in Go?

I assume you want a pointer because you want to allow "missing time" option, which is necessary for the encoding/json package to respect a "missing time" (details: https://stackoverflow.com/questions/32643815/json-omitempty-with-time-time-field/32646035#32646035). Note that in other cases it may not be necessary to automatically turn to pointer: you may use the zero value of time.Time (which is a struct) to indicate the "missing time" state. To easily tell if a time.Time is its zero value, you may use Time.IsZero(). See related: https://stackoverflow.com/questions/52216908/idiomatic-way-to-represent-optional-time-time-in-a-struct/52217049#52217049

答案2

得分: 2

time.Now() 返回一个 Time 值,但你的结构体需要一个指针。将你的结构体改为:

type Payload struct {
    ...
    Timestamp *time.Time `json:"timestamp,omitempty"`
    ...
}

根据文档(https://pkg.go.dev/time#Time)的说明:

使用时间的程序通常应将其存储和传递为值,而不是指针。也就是说,时间变量和结构体字段应该是 time.Time 类型,而不是 *time.Time。

英文:

time.Now() returns a Time value, but your struct is looking for a pointer. Change your struct to

type Payload struct {
    ...
    Timestamp time.Time  `json:"timestamp,omitempty"`
    ...
}

from the docs (https://pkg.go.dev/time#Time):

> Programs using times should typically store and pass them as values, not pointers. That is, time variables and struct fields should be of type time.Time, not *time.Time.

huangapple
  • 本文由 发表于 2021年9月1日 03:06:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/69004123.html
匿名

发表评论

匿名网友

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

确定