Skip to content

Commit ec9cf88

Browse files
authored
Update example in README (#19)
1 parent b884a93 commit ec9cf88

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

README.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,25 @@ Duration Check
77

88
A Go linter to detect cases where two `time.Duration` values are being multiplied in possibly erroneous ways.
99

10-
For example, consider the following (highly contrived) function:
10+
Consider the following (highly contrived) code:
1111

1212
```go
13-
func waitFor(someDuration time.Duration) {
14-
timeToWait := someDuration * time.Second
15-
time.Sleep(timeToWait)
13+
func waitForSeconds(someDuration time.Duration) {
14+
timeToWait := someDuration * time.Second
15+
fmt.Printf("Waiting for %s\n", timeToWait)
16+
}
17+
18+
func main() {
19+
waitForSeconds(5) // waits for 5 seconds
20+
waitForSeconds(5 * time.Second) // waits for 1388888h 53m 20s
1621
}
1722
```
1823

19-
Although the above code would compile without any errors, its runtime behaviour would almost certainly be incorrect.
20-
A caller would reasonably expect `waitFor(5 * time.Seconds)` to wait for ~5 seconds but they would actually end up
21-
waiting for ~1,388,889 hours.
24+
Both invocations of the function are syntactically correct but the second one is probably not what most people want.
25+
In this contrived example it is quite easy to spot the mistake. However, if the incorrect `waitForSeconds` invocation is
26+
nested deep within a complex piece of code that runs in the background, the mistake could go unnoticed for months (which
27+
is exactly what happened in a production backend system of fairly well-known software service).
2228

23-
The above example is just for illustration purposes only. The problem is glaringly obvious in such a simple function
24-
and even the greenest Gopher would discover the issue immediately. However, imagine a much more complicated function
25-
with many more lines and it is not inconceivable that such logic errors could go unnoticed.
2629

2730
See the [test cases](testdata/src/a/a.go) for more examples of the types of errors detected by the linter.
2831

0 commit comments

Comments
 (0)