来自time.Time的意外输出 [英] Unexpected output from time.Time

查看:60
本文介绍了来自time.Time的意外输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是通过观看有关Udemy的教学视频开始学习Go的,我尝试按如下方式打印当前时间

I just started to learn Go by following a tutorial video on Udemy, and I tried to print the current time as below

import (
  "fmt" 
  "time"
)

func main(){
  t := time.Now()

  fmt.Println(t) 
}

我得到一个很长的文本,如下所示

And I get a very long text as the output as below

2018-07-04 12:03:07.2911671 +0800 +08 m=+0.002000201

我期望只得到+0800,然后是timeZone,这应该是结尾.预期的输出如下所示,也如本教程视频中所示.但是对我来说,结果的形式要长得多.

I was expecting to get only the +0800 followed by a timeZone and that should be the end of it. The expected output is shown below and as it was shown in the tutorial video, too. But for me, the result is in much longer form.

2018-07-04 12:03:07.2911671 +0530 IST

问题是,为什么相同的命令date.Now()在讲师的程序和我的程序之间返回不同的格式?为什么没有设置任何特定格式,不应该返回standardize/base格式?

The question is, why does the same command date.Now() return different formats between the instructor's program and mine? Why is there no specific format being set, shouldn't a standardize/base format being returned instead?

推荐答案

问题是,为什么相同的命令date.Now()在讲师的程序和我的程序之间返回不同的格式?

The question is, why the same command date.Now() is returning different format between the instructor's program and mine?

因为该教程是在Go 1.9发行之前创建的.从1.9版开始,time.Time结构中添加了单调时钟支持,添加了这些额外的字段.

Because the tutorial was created before the release of Go 1.9. As of Go 1.9, monotonic clock support was added to the time.Time struct, which added those extra fields.

对于正常使用,您应该始终使用 Format 函数输出时间,而不是输出原始数据.这将产生更多有用的输出,并防止将来在基础类型上添加任何内容.

For normal usage, you should always output time using the Format function, rather than outputting the raw data. This will produce more useful output, and be protected against any future additions to the underlying type.

这篇关于来自time.Time的意外输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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