Skip to content

proposal: time: enable warnings when doing time.Duration multiplication #64420

@vax-r

Description

@vax-r

Proposal Details

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

set_time, err := time.ParseDuration(os.Getenv("WAIT_TIME"))
if err != nil {
    panic(err)
}
time.Sleep(set_time * time.Second)

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions