3 ms·
time.Duration is a count of nanoseconds, and time.Millisecond is a constant that represents the number of nanoseconds in a millisecond. http://golang.org/pkg/t
by Titanous 13y ago
time.Duration is a count of nanoseconds, and time.Millisecond is a constant that represents the number of nanoseconds in a millisecond.
http://golang.org/pkg/time/#Duration http://golang.org/pkg/time/#Duration
- AaronFriel 13y agoThis is the sort of thing having a Haskell-like typesystem would fix - multiplying something by "time.Millisecond" is difficult to discern the semantic meaning of, especially when nothing in the code/language prevents you from doing it multiple times. The idea of storing this information as just a regular Int64 bothers me after spending so much time in type system nirvana.
- dragonwriter 13y ago> The idea of storing this information as just a regular Int64 bothers me after spending so much time in type system nirvana. Its not a regular int64, its a distinct named type (Duration) where int64 is the underyling time. I'm still getting familiar with Go -- and don't have it on the machine I'm using at the moment -- but from the docs named types are incompatible with other named types with the same underlying types in numerous contexts, but it looks like one place where named types are leaky abstractions is their interaction with operators.
- dragonwriter 13y agoTo illustrate this point, this code: package main import ( "time" "fmt" ) func ident(x int64) int64 { return x } func main() { t := time.Duration(1) v := math.Inf(t) } produces the error: cannot use t (type time.Duration) as type int64 in function argument The issue is with Go's arithmetic operators subverting the type system more than with the type system itself.