How to declare Time in Go?

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

How to declare Time in Go?

问题

在Go语言中,可以使用time包来声明时间类型的变量。要声明一个时间变量,可以使用time.Time类型。例如:

import "time"

func main() {
    var currentTime time.Time
    // 使用time.Now()函数获取当前时间
    currentTime = time.Now()
    // 打印当前时间
    fmt.Println(currentTime)
}

在上面的示例中,我们声明了一个名为currentTime的时间变量,并使用time.Now()函数将当前时间赋值给它。然后,我们打印出这个时间变量的值。

你可以根据需要使用不同的time包函数和方法来操作时间变量。希望这可以帮助到你!

英文:

I have been walking through the tutorials and know how to declare variables but can not find how to declare of variable as type Time.

How Time is declared in Go?

答案1

得分: 2

你会在time包中找到很多关于Time变量声明的示例,就像在Duration示例中一样。

 t0 := time.Now()

这是使用"短变量声明",它是一个简写形式的常规变量声明,没有类型,只有初始化表达式。

英文:

You will find plenty of example of Time variable declaration in the package time itself, as in the Duration example

 t0 := time.Now()

This is using the "short variable declaration", which is a shorthand for a regular variable declaration with initializer expressions but no types.

huangapple
  • 本文由 发表于 2014年9月9日 21:35:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/25746061.html
匿名

发表评论

匿名网友

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

确定