2

Can someone tell me why this works:

s := time.Hour/73.0
fmt.Println("sleeping: ", s)
time.Sleep(s)

But this fails:

d := 73.0
s := time.Hour/d
fmt.Println("sleeping: ", s)
time.Sleep(s)

This is the error:

invalid operation: time.Hour / d (mismatched types time.Duration and float64)
icza
  • 389,944
  • 63
  • 907
  • 827
rumdrums
  • 1,322
  • 2
  • 11
  • 25

1 Answers1

6

This line:

s := time.Hour/73.0

Is a short variable declaration, where the right hand side expression is: time.Hour / 73.0. time.Hour is a typed constant from the time package, its type is time.Duration; and 73.0 is an untyped numeric constant which when used in an expression, will take the appropriate type if possible. Since time.Duration has int64 as its underlying type, the constant 73.0 can be converted to time.Duration without loss of precision, so that will happen, and the constant expression time.Hour / time.Duration(73.0) will be executed.

The other line:

d := 73.0
s := time.Hour/d

The first line is a short variable declaration, where the type of d will be inferred from the right hand side expression, which is an untyped numeric constant (given with a floating point literal to be exact). Since a type is needed, its default type will be used, which is float64. So d will be a variable of type float64.

The short variable declaration in the next line: time.Hour / d, here time.Hour is again a value of type time.Duration, but you attempt to divide it by a value of type float64, that is not allowed in Go.

To make it work, you have to explicitly convert d to time.Duration:

s := time.Hour / time.Duration(d)

Or d must be of type time.Duration:

d := time.Duration(73.0)
s := time.Hour / d

Or:

var d time.Duration = 73.0
s := time.Hour / d

These wouldn't work if the value cannot be represented with time.Duration, for example 73.5 (because time.Duration has int64 underlying type). In this case you have to convert time.Hour to float64, perform the division and convert the result to time.Duration, e.g.:

d := 73.5
s := time.Duration(float64(time.Hour) / d)

Read more details on the topic:

Spec: Constants

The Go Blog: Constants

Why does 0.1 + 0.2 get 0.3 in Google Go?

How does Go perform arithmetic on constants?

Golang converting float64 to int error

Community
  • 1
  • 1
icza
  • 389,944
  • 63
  • 907
  • 827
  • Thanks very much for the the detailed answer and links to further reading. I learned a lot from this! – rumdrums Mar 05 '17 at 21:42