Convert Duration in Micro and nano second

package main

import (
“fmt”
“time”
)

func main() {

start := time.Now()
time.Sleep(1000 * time.Millisecond)
   end:=time.Now()
diff := end.sub(start)

}

how to print the diff value in second, milisecond, microsecond and nanosecond?

Check out the methods available to time.Duration (what diff in your code is): https://godoc.org/time#Duration

check this code:

package main

import (
"fmt"
"time"
)

func main() {
    t0 := time.Now()
    time.Sleep(1000 * time.Millisecond)
    x:=time.Now().Sub(t0)
    fmt.Printf("%v %vms %vus %vns\n",
        x,
        int64(x/time.Millisecond),
        int64(x/time.Microsecond),
        int64(x/time.Nanosecond),
        )
}
4 Likes

Thanks a lot… This is exactly I was looking for

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.