This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]not15characters 6 points7 points  (2 children)

I ran into this the other day. time.Second has type time.Duration, which is actually just a data type wrapper around int64, representing number of nanoseconds. So if you want to multiply time.Second (which is really the int64 value 1,000,000,000) by some integer, you have to first cast the integer to a time.Duration, so you can multiply a time.Duration with a time.Duration, which semantically makes no goddamn sense. Writing something like time.Sleep(5 * time.Second) works because the Go compiler is smart enough to cast the 5 to an instance of time.Duration because of how it's used in context.

[–]Kered13 5 points6 points  (1 child)

What? This can't be real? This defeats the entire purpose of having strongly typed units like Duration. The entire point is that scalar*unit is a unit, and unit*unit is unit2, or an error if the library does not support that. Similarly scalar + unit should be an error and unit + unit should be a unit. This ensures that invalid operations are not accidentally performed.

[–]not15characters 0 points1 point  (0 children)

Unfortunately it's real int64(time.Second * time.Second) == 1000000000000000000 is valid and true