首页 > 其他分享 >Go - Representing Duration

Go - Representing Duration

时间:2023-10-05 18:33:08浏览次数:32  
标签:Hour just Duration want time Go Representing methods

Problem: You want to specify a duration of time.


Solution: Use the Duration type to represent a span of time.

 

The main representation for a span of time in the time package is, of course,
Duration . Duration is nothing fancy, though; it’s just an int64 with some interesting methods. You normally create a Duration like this:

d   :=   2   *   time . Hour

This creates two hours. You can find the pretty form by calling the String method, or if you simply want to print it out to the screen, just do this:

fmt . Println ( d )

and you should see this:

2h0m0s

What if you want to create 2 hours, 34 minutes, and 5 seconds? Just add them together, of course (since Duration is just an int64 ):

d   :=   ( 2   *   time . Hour )   +   ( 34   *   time . Minute )   +   ( 5   *   time . Second )

If you want to find out the equivalent in minutes, you can do this:

d . Minutes ()

Or in seconds, milliseconds, and so on using the respective methods. However, there is no equivalent for anything larger than time.Hour .

 

标签:Hour,just,Duration,want,time,Go,Representing,methods
From: https://www.cnblogs.com/zhangzhihui/p/17743738.html

相关文章

  • MongoDB高阶特性:事务、索引
    一、事务一)MongoDB的事务首先我们需要知道MongoDB是有多种存储引擎的,不同的存储引擎在实现ACID的时候,使用不同的机制。而Mongodb从3.0开始默认使用的是WiredTiger引擎,本文后续所有文字均是针对WiredTiger引擎。WiredTiger引擎可以针对单个文档来保证ACID特性,但是当需要操作多个......
  • Go - Representing Time Zones
    Problem: YouwanttoincludethetimezoneinformationinaTimestruct.Solution: TheTimestructincludesaLocation,whichistherepresentationofthetimezone. Atimezoneisanareathatfollowsastandardtimethatroughlyfollowslongitudebu......
  • MongoDBHelper + Expression+ JsonResult
    usingMongoDB.Driver;usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Linq.Expressions;namespaceMongodbTest.Common{///<summary>///MongoDb帮助类///</summary>publicclassMongoDbHelper......
  • vue-router.esm.js:2065 Uncaught (in promise) Error: Redirected when going from "
    原因:  vue-router路由版本更新产生的问题,导致路由跳转失败抛出该错误;真正的原因是由于返回了一个Promise对象,正常的跳转由then方法执行当正常的路由跳转,被"路由导航守卫"拦截并重新指定路由时,由于this.$router.push()返回的是Promise对象,此时then方法不能正常执......
  • kubelet.go 2466 Error getting node not found
    kubeadm初始化kubernetes集群报错,kubelet服务提示:kubelet.go:2466]"Errorgettingnode"err="node\"k8s3-master\"notfound处理记录。  0.ENV kubernetes1.22.x/1.23.x/1.24.x(不限于所列版本)CentOS7.x/Ubuntu22.04(不限于所列版本)  1.问题现象 kube......
  • Go - Decoding Data with a Customized Binary Format to Structs
    Problem: Youwanttodecodethecustomizedbinaryformatbacktostructs.Solution: Usetheencoding/binarypackagetotakedatafromthebinaryformatand reconstructstructsfromit. funcmain(){vardataMeterfile,err......
  • Go - Encoding Data to a Customized Binary Format
    Problem: Youwanttoencodestructdatatoacustomizedbinaryformat.Solution: Designyourcustomizedformatandusetheencoding/binarypackagetowritedatainstructstoit. Usinggobhasacoupleofdrawbacks.First,gobissupportedbyGoonlya......
  • Go - Decoding gob Format Data to Structs
    Problem: Youwanttodecodegobformatdatabacktostructs.Solution: Usetheencoding/gobpackagetodecodethegobformatdatabacktostructs. funcread(datainterface{},filenamestring){file,err:=os.Open(&quo......
  • Go - Encoding Data to gob Format Data
    Problem: Youwanttoencodestructsintobinarygobformat.Solution: Usetheencoding/gobpackagetoencodethestructsintobytesthatcanbestoredorsentelsewhere. Theencoding/gobpackageisaGolibrarytoencodeanddecodeabinaryformat.The......
  • 工具 | 极其方便的谷歌翻译软件 Myna for Google Translate for Mac | Mac
    工具|极其方便的谷歌翻译软件MynaforGoogleTranslateforMac|Mac前言Mac哪款翻译软件好用呢?市面有太多的翻译工具了,如:百度、谷歌、有道等等。但是不得不说作为对外交流学习或学术阅览,谷歌翻译算得上是比较专业和让人信赖的。而MynaforGoogleTranslateforMac是......