dougan0529 2017-09-11 03:05
浏览 47
已采纳

为什么Go中的时间在结构中以不同的方式打印?

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
", 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
", 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?
  • 写回答

2条回答 默认 最新

  • driuwt9557 2017-09-11 04:18
    关注

    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
    ", 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
    ", 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)