Calculate Standard Errors on time.Duration

I need to compute some (elementary) statistics on durations. Although I like time implementation in Go and i need the duration functions to determine a time lapse, and I could add and multiply (although not elegantly) durations, I find myself in a dead end when i want to compute the Square Root. The only solution I see is to convert to float64 (But how convert any time lapse in float64 ?)

Thank you

A time.Duration is based in int64
type Duration [int64]
So you can convert to float justo doing a cast operation to float64. For example

package main

import (

func expensiveCall() {
   time.Sleep(100 * time.Millisecond)

func main() {
	t0 := time.Now()
	t1 := time.Now()
	elapsed := t1.Sub(t0)
	fmt.Printf("The call took %v to run.\n", elapsed)
	fmt.Printf("In float=%f", float64(elapsed))

Thank you. The trick is to cast duration directly into float64. And recast into time.Duration when the computation is done.

Or, use the Seconds() method to get a reasonable unit as a float64.

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