Proposal for change type Duration int64

80 views
Skip to first unread message

Nikhilesh Susarla

unread,
Nov 22, 2022, 5:28:15 AM11/22/22
to golang-nuts
Hi, 

type Duration int64

The current Duration is int64 and the duration value should never be less than 0 else it will panic. It would be safe and advisable to change it to uint64 or so, where at least it would not cause panic. 

Is there any reason for not using uint?

Thank you
Susarla Nikhilesh

Volker Dobler

unread,
Nov 22, 2022, 5:33:58 AM11/22/22
to golang-nuts
On Tuesday, 22 November 2022 at 11:28:15 UTC+1 nikhil...@gmail.com wrote:
[...] the duration value should never be less than 0 else it will panic
This statement is wrong, a negative time.Duration doesn't panic.
(Maybe you are talking about time.Sleep?)

Is there any reason for not using uint?
Because negative durations are common and useful.

V.

Shulhan

unread,
Nov 22, 2022, 5:38:46 AM11/22/22
to Nikhilesh Susarla, golang-nuts
A negative duration is required to compute time.Time-d, using
Time.Add(-Duration) [1].
Also, Time.Sub may return negative too.

[1] https://pkg.go.dev/time#Time.Sub

Nikhilesh Susarla

unread,
Nov 22, 2022, 5:43:14 AM11/22/22
to golang-nuts
Got it. I was wrong. 
time.NewTicker would panic if duration is negative.

But if the duration is used widely in negative then fine :) 

Thank you for correcting 

Reply all
Reply to author
Forward
0 new messages