This question already has an answer here:
I'm getting an invalid operation: *timeout * time.Second (mismatched types int and time.Duration)
error when trying to run something similar to this
timeout := flag.Int("timeout", 30, "The time limit for answering questions.")
flag.Parse()
timeoutCh := time.After(*timeout * time.Second)
Just to be sure, I checked the type of *timeout
using reflect.TypeOf()
and it is in fact an int
. But if I do timeoutCh := time.After(30 * time.Second)
or use any other int
value the code works.
What am I missing here?
timeoutCh := time.After(time.Duration(*timeout) * time.Second)
You have to convert *timeout
which is type int
to type time.Duration
. The reason why time.After(30 * time.Second)
works is that 30
is untyped and is converted to the type of time.Second
which is time.Duration
. See https://golang.org/ref/spec#Operators. Similarly this code works
x := uint(42)
if x == 42 {
println("works!")
}
but this code won't compile
x := uint(42)
y := 42 // defaults to type int
if x == y {
println("this won't complile!")
}
You cannot multiply two different types, so you will need to convert the integer to a time.Duration type. You can do that by simply casting it like this:
time.Duration(*timeout)
A "unit" of time is technically a nanosecond and time.Second is one seconds worth of nanoseconds. The math works out though so you can simply say something like this for 3 seconds:
time.Duration(3) * time.Second