为什么Go中的时间在结构中以不同的方式打印? [英] Why is time in Go printed differently in a struct?

查看:45
本文介绍了为什么Go中的时间在结构中以不同的方式打印?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用Go,在我编写的第一个程序中,我打印了一个结构,该结构也显示了

  {wall:0 ext:63533980800 loc:< nil>}} 

对似乎是 time.Time()类型的东西感到困惑,然后通过Google搜索将我带到

因此,要进行隔离测试,我创建了一个新的简约程序:

 程序包主要进口 ("fmt"时间")输入TheStruct struct {时间时间}func main(){the_struct:= TheStruct {time.Now()}fmt.Println(the_struct)fmt.Printf(%+ v \ n",the_struct)fmt.Println(the_struct.the_time)fmt.Println()the_struct_2:= TheStruct {time.Unix(1505099248,200)}fmt.Println(the_struct_2)fmt.Printf(%+ v \ n",the_struct_2)fmt.Println(the_struct_2.the_time)} 

打印出以下内容:

  {{13719544904843884912 534246 0x1140680}}{the_time:{wall:13719544904843884912 ext:534246 loc:0x1140680}}2017-09-11 05:08:11.35635032 +0200 CEST m = + 0.000534246{{200 63640696048 0x1140680}}{the_time:{wall:200 ext:63640696048 loc:0x1140680}}2017-09-11 05:07:28 +0200 CEST 

所以我想知道这里的两件事:

  1. 为什么单独打印(使用 the_struct.the_time )时,将结构的一部分打印为壁钟而不是更常用的日期时间符号的时间呢?
  2. 我其他程序中的代码为该位置打印出< nil> 是一个问题吗?我将如何解决这个问题?

在您的结构中不打印格式化时间的原因是未对未导出的字段调用String方法(请参阅

输出:

  {2009-11-10 23:00:00 +0000 UTC m = + 0.000000000}{The_time:2009-11-10 23:00:00 +0000 UTC m = + 0.000000000}2009-11-10 23:00:00 +0000 UTC m = + 0.000000000{2017-09-11 03:07:28.0000002 +0000 UTC}{The_time:2017-09-11 03:07:28.0000002 +0000 UTC}2017-09-11 03:07:28.0000002 +0000 UTC 

在操场上: https://play.golang.org/p/r0rQKBlpWc

I'm just starting out with Go, and in the first program I wrote I printed out a struct, which also showed

{wall:0 ext:63533980800 loc:<nil>}

Being puzzled over what that was it seemed to be a type time.Time(), and a google search brought me to this part of the Go source code in which the difference between the "wall clock" and the "monotonic clock" is explained in the comments.

So to test it in isolation I created a new minimalistic program:

package main

import (
    "fmt"
    "time"
)

type TheStruct struct {
    the_time time.Time
}

func main() {
    the_struct := TheStruct{time.Now()}
    fmt.Println(the_struct)
    fmt.Printf("%+v\n", the_struct)
    fmt.Println(the_struct.the_time)
    fmt.Println()
    the_struct_2 := TheStruct{time.Unix(1505099248, 200)}
    fmt.Println(the_struct_2)
    fmt.Printf("%+v\n", the_struct_2)
    fmt.Println(the_struct_2.the_time)
}

which prints out the following:

{{13719544904843884912 534246 0x1140680}}
{the_time:{wall:13719544904843884912 ext:534246 loc:0x1140680}}
2017-09-11 05:08:11.35635032 +0200 CEST m=+0.000534246

{{200 63640696048 0x1140680}}
{the_time:{wall:200 ext:63640696048 loc:0x1140680}}
2017-09-11 05:07:28 +0200 CEST

So I wonder about two things here:

  1. Why is the time if part of a struct printed as wall clock as compared to the more usual datetime notation when printed out separately (using the_struct.the_time)?
  2. Is it a problem that the code in my other program prints out <nil> for the loc? How would I be able to solve that?

解决方案

The reason why it's not printing the formatted time when in your struct is that String method is not invoked on the unexported fields (refer https://golang.org/pkg/fmt/):

When printing a struct, fmt cannot and therefore does not invoke formatting methods such as Error or String on unexported fields.

Changing your structure to export fields (capitalizing the first letter) makes it invoke the String method:

   package main

import (
    "fmt"
    "time"
)

type TheStruct struct {
    The_time time.Time
}

func main() {
    the_struct := TheStruct{time.Now()}
    fmt.Println(the_struct)
    fmt.Printf("%+v\n", the_struct)
    fmt.Println(the_struct.The_time)
    fmt.Println()
    the_struct_2 := TheStruct{time.Unix(1505099248, 200)}
    fmt.Println(the_struct_2)
    fmt.Printf("%+v\n", the_struct_2)
    fmt.Println(the_struct_2.The_time)
}

Output:

{2009-11-10 23:00:00 +0000 UTC m=+0.000000000}
{The_time:2009-11-10 23:00:00 +0000 UTC m=+0.000000000}
2009-11-10 23:00:00 +0000 UTC m=+0.000000000

{2017-09-11 03:07:28.0000002 +0000 UTC}
{The_time:2017-09-11 03:07:28.0000002 +0000 UTC}
2017-09-11 03:07:28.0000002 +0000 UTC

On playground : https://play.golang.org/p/r0rQKBlpWc

这篇关于为什么Go中的时间在结构中以不同的方式打印?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆