You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
As we are using time.Sleep() function, we often just throw in an integer like the following
time.Sleep(5 * time.Second)
What if we throw a type Duration with some time units like 's' instead of an integer?
I've done this before when trying to set some time arguments in environment variable and read it into a Go program, like the following
Basically WAIT_TIME is about 5s, and the program will wait for a super long time.
I guess the reason is that we've done something like time.Duration * time.Second in set_time * time.Second, since set_time is basically 5s with type time.Duration, which is meaningless in this kind of situation (we're not dealing with acceleration)
time.Duration is in fact int64, so I think the compiler may just think this behavior as int64 * int64, but it doesn't make sense here
I suggest we should add some detecting mechanism to prevent this kind of undefined behavior from happening, what do you think?
If you guys think it makes sense I can start to work on it.
The text was updated successfully, but these errors were encountered:
vax-r
changed the title
proposal: src/time/time.go: enable warnings when doing time.Duration multiplication
proposal: time: enable warnings when doing time.Duration multiplication
Nov 28, 2023
Proposal Details
As we are using
time.Sleep()
function, we often just throw in an integer like the followingWhat if we throw a type Duration with some time units like 's' instead of an integer?
I've done this before when trying to set some time arguments in environment variable and read it into a Go program, like the following
Basically
WAIT_TIME
is about5s
, and the program will wait for a super long time.I guess the reason is that we've done something like
time.Duration * time.Second
inset_time * time.Second
, sinceset_time
is basically5s
with typetime.Duration
, which is meaningless in this kind of situation (we're not dealing with acceleration)time.Duration
is in factint64
, so I think the compiler may just think this behavior asint64 * int64
, but it doesn't make sense hereI suggest we should add some detecting mechanism to prevent this kind of undefined behavior from happening, what do you think?
If you guys think it makes sense I can start to work on it.
The text was updated successfully, but these errors were encountered: