2

I am trying to calculate a time to sleep but am stuck. I can sleep for 10 seconds:

time.Sleep(10 * time.Second)

However, I try the following:

package main

import (
    "log"
    "time"
    "reflect"
)

func main() {
    n := time.Now()
    remaining := 10 - int(time.Since(n).Seconds())
    log.Println(reflect.TypeOf(10), reflect.TypeOf(remaining))
    //time.Sleep(10 * time.Second)
    time.Sleep(remaining * time.Second)
}

I get:

invalid operation: remaining * time.Second (mismatched types int and time.Duration)

remaining is an int, just as 10 is an int. What gives?

My playground

  • the accepted answer linked to does not work....http://play.golang.org/p/jANhmUA_Gw –  Feb 22 '16 at 01:29
  • 2
    Both operands need to be the same type. Example: https://play.golang.org/p/E4RLQEDIWo –  Feb 22 '16 at 01:38
  • put that as an answer and I'll accept. –  Feb 22 '16 at 02:29

0 Answers0