JIYIK CN >

Current Location:Home > Learning > PROGRAM > Go >

Duration in Go

Author:JIYIK Last Updated:2025/04/15 Views:

Go duration conversion can be done in a variety of ways. The time library and the time.duration method are often used to calculate and display time.

Note that duration refers to the time elapsed between two defined time objects, as an int64 count of nanoseconds.


Duration Instance Conversion

   
Nanosecond 1
Microsecond 1000 * Nanosecond
Millisecond 1000 * Microsecond
Second 1000 * Millisecond
Minute 60 * Second
Hour 60 * Minute

Using the time.duration Method

package main

import (
    "fmt"
    "time"
)

func main() {
    d, err := time.ParseDuration("1h25m10.9183256645s")
    if err != nil {
        panic(err)
    }

    count := []time.Duration{
        time.Nanosecond,
        time.Microsecond,
        time.Millisecond,
        time.Second,
        2 * time.Second,
        time.Minute,
        10 * time.Minute,
        time.Hour,
    }

    for _, r := range count {
        fmt.Printf("(%6s) = %s\n", r, d.Round(r).String())
    }
}

Output:

(   1ns) = 1h25m10.918325664s
(   1µs) = 1h25m10.918326s
(   1ms) = 1h25m10.918s
(    1s) = 1h25m11s
(    2s) = 1h25m10s
(  1m0s) = 1h25m0s
( 10m0s) = 1h30m0s
(1h0m0s) = 1h0m0s

The above code allows us to calculate all corresponding durations as floating point integers.

Calculate running time using the time library

package main
import (
    "fmt"
    "time"
)
func main() {

    var t time.Duration = 100000000000000
    fmt.Println(t.Hours())
    fmt.Println(t.Minutes())
    fmt.Println(t.Seconds())

    now := time.Now()
    time.Sleep(100000)
    diff := now.Sub(time.Now())

    fmt.Println("Elapsed time in seconds: ", diff.Seconds())
}

Output:

27.77777777777778
1666.6666666666667
100000
Elapsed time in seconds:  -0.0001

The elapsed time tells us how long it took the processor to calculate the duration.

Convert a duration to a number in Go

package main
import (
    "fmt"

    "time"
)
func main() {

timeInput := 3600

data := time.Duration(timeInput) * time.Millisecond

fmt.Println("The time in Nanoseconds:", int64(data/time.Nanosecond))

fmt.Println("The time in Microseconds:", int64(data/time.Microsecond))

fmt.Println("The time in Milliseconds:", int64(data/time.Millisecond))
}

Output:

The time in Nanoseconds: 3600000000
The time in Microseconds: 3600000
The time in Milliseconds: 3600

The above code allows us to convert a number to a time instance, then time.Duration performs the time conversion. Finally, the time instance is converted back to a number.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Install GoLang using Brew

Publish Date:2025/04/15 Views:82 Category:Go

This article describes how to install GoLang using Brew on Linux or macOS. Install GoLang using Brew brew installs missing packages in Linux and macOS. It makes it easy to install GoLang on Linux or macOS. Follow the steps below to install

GoLang RWMutex Detailed Introduction

Publish Date:2025/04/15 Views:116 Category:Go

This article introduces how to use rwmutex in Go language. Go language RWMutex mutex is the abbreviation of mutual exclusion, which is used to keep track of which thread has accessed a variable at any time. Mutex is a data structure provide

Enabling CORS in GoLang

Publish Date:2025/04/15 Views:102 Category:Go

This article describes how to enable and use CORS in GoLang. Go language CORS Cross-origin resource sharing (CORS) is a process based on HTTP headers that defines the origins from which browsers are allowed to load and use resources. CORS i

Function pointers in Go

Publish Date:2025/04/15 Views:83 Category:Go

A pointer in Go is a special-purpose variable that stores the memory address of other variables and the point where the memory is located. It can also access the value stored in that memory location. Pointer declaration in Go var pointer_na

Getting a string representation of a structure in Go

Publish Date:2025/04/15 Views:63 Category:Go

Go allows us to serialize data from structures using a variety of simple standard methods. Converting a structure to a string using String method in Go The GoLang package String helps implement simple functions to manipulate and edit UTF-8

Convert JSON to struct in Go

Publish Date:2025/04/15 Views:126 Category:Go

This article describes how to convert JSON to struct in GoLang . Convert JSON to Struct using Unmarshal method in Go The encoding/json package of the Go language provides a function Unmarshal to convert JSON data into byte format. This func

Golang 中的零值 Nil

Publish Date:2023/04/27 Views:185 Category:Go

本篇文章介绍 nil 在 Golang 中的含义,nil 是 Go 编程语言中的零值,是众所周知且重要的预定义标识符。

Golang 中的 Lambda 表达式

Publish Date:2023/04/27 Views:699 Category:Go

本篇文章介绍如何在 Golang 中创建 lambda 表达式。Lambda 表达式似乎不存在于 Golang 中。 函数文字、lambda 函数或闭包是匿名函数的另一个名称。

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial