3

【Golang】理解Golang中的time.Duration

 2 years ago
source link: https://studygolang.com/articles/35777
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

【Golang】理解Golang中的time.Duration

taoshihan1991 · 大约18小时之前 · 231 次点击 · 预计阅读时间 1 分钟 · 大约8小时之前 开始浏览    

在传递时间参数的时候,很多函数的参数类型是time.Duration

官方解释:

Duration 将两个瞬间之间的经过时间表示为 int64 纳秒计数。 该表示将最大可表示持续时间限制为大约 290 年。

默认是纳秒单位

如果想传递一个10秒的时间进去,需要这样转换,其实就是把我们传递的整型进行了乘法

second := 10

time.Duration(seconds)*time.Second

time.Second是一个常量

const (
    Nanosecond  Duration = 1
    Microsecond          = 1000 * Nanosecond
    Millisecond          = 1000 * Microsecond
    Second               = 1000 * Millisecond
    Minute               = 60 * Second
    Hour                 = 60 * Minute
)

time.Duration(seconds) 是进行的类型转换,把我们的整型转换成了time.Duration类型

然后把我们传递的10 * 1000 * 1000 ,这样就是我们想要的结果了

GOFLY是一款基于Golang+Vue开发的在线客服系统,软件著作权编号:2021SR1462600。一套可私有化部署的在线客服系统,编译后的二进制文件可直接使用无需搭开发环境,下载zip解压即可,仅依赖MySQL数据库,是一个开箱即用的网页在线客服系统,致力于帮助广大开发者/中小站长快速整合私有客服功能。

github地址:go-fly

官网地址:https://gofly.v1kf.com


有疑问加站长微信联系(非本文作者)

280

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK