7

Why does time.Sleep(5 * time.Second) work fine, but:

x := 180
time.Sleep(15 / x * 60 * time.Second)

does not? I get a type mismatch error (types int64 and time.Duration). Given the error, I understand more of why the latter fails than why the former succeeds.

Jeff Erickson
  • 3,783
  • 8
  • 36
  • 43
  • 2
    Possible duplicate of [Conversion of time.Duration type microseconds value to milliseconds](https://stackoverflow.com/questions/41503758/conversion-of-time-duration-type-microseconds-value-to-milliseconds/41503910#41503910). – icza Mar 26 '18 at 18:51

1 Answers1

21

In Go, a numeric literal (e.g. 60) is an untyped constant. That means it will be silently coerced to whatever type is appropriate for the operation where it's being used. So when you say:

var x := 5 * time.Second

Then the type is inferred from time.Second to be a time.Duration, and thus the literal 5 is also treated as a time.Duration. If there's nothing to infer a type from, it will assume a type ("bool, rune, int, float64, complex128 or string") and use that. So:

x := 180

Yields x with a type of int.

However, when you do some operation involving something with a type - like, say a variable x that is an int - then you have two types and one must be converted for the operation to be legal.

So, to the original question "When does int * time.Second work and when does it not in golang?", int * time.Second actually never works in Go. But 5 * time.Second isn't the same as int * time.Second.

This is touched on in the Go tour:

An untyped constant takes the type needed by its context.

Adrian
  • 42,911
  • 6
  • 107
  • 99