title: Creating a time.Duration from float64 seconds
body: <p>I have a <code>float64</code> containing a duration in seconds. I'm looking for a way to convert this value to a <code>time.Duration</code>. I'm able to perform this conversion, but I'm wondering if there is not a more elegant way.</p>
  <p>The approach I have is this:</p>
  <pre><code>    var timeout float64 // input value of float type
  
      var res time.Duration // result value of time.Duration type
  
      res += time.Duration(math.Round(timeout)) * time.Second
      timeout -= math.Round(timeout)
      timeout *= 1000
      res += time.Duration(math.Round(timeout)) * time.Millisecond
      timeout -= math.Round(timeout)
      timeout *= 1000
      res += time.Duration(math.Round(timeout)) * time.Microsecond
      timeout -= math.Round(timeout)
      timeout *= 1000
      res += time.Duration(math.Round(timeout)) * time.Nanosecond
  
      return res
  </code></pre>
  <p>What I dislike about this is that it is cumbersome and not reliable. I'd expect Go to supply something like this out of the box and to perform these conversions in a way that detects overflows and similar range violations. It seems that <a href="https://github.com/golang/go/issues/20757" rel="noreferrer">Go is not there yet</a>, but maybe I missed something obvious, hence my question.</p>
  <p>Notes:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/41503758/conversion-of-time-duration-type-microseconds-value-to-milliseconds">This question</a> doesn't address my needs, because it is rather related to the opposite way of conversion. That conversion is actually pretty painless, which makes the it even more surprising that the one I need isn't there.</li>
  <li>Why don't I use milliseconds instead? Simple reason: Consistency, KISS principle, principle of least surprise. The SI unit for time is the second. Everything else is only derived from this, so I use this as a default.</li>
  <li>Nitpick concerning the previous statement: Go itself says &quot;There is no definition for units of Day or larger to avoid confusion across daylight savings time zone transitions.&quot;. They missed the point, because they still have minutes and hours, even though there are minutes with 58..61 seconds. Not a big deal, just mentioning it for completeness.</li>
  </ul>