diff options
author | Russ Cox <rsc@golang.org> | 2020-05-21 20:53:45 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2020-05-22 11:28:55 +0000 |
commit | 9acdc705e7d53a59b8418dd0beb570db35f7a744 (patch) | |
tree | cf6e538b6845b0e20047da61623d93d0a4b44840 | |
parent | ea2de3346ff8714c9fae95c2a3d686ec07d47ca8 (diff) | |
download | go-9acdc705e7d53a59b8418dd0beb570db35f7a744.tar.gz go-9acdc705e7d53a59b8418dd0beb570db35f7a744.zip |
time: simplify Duration.String example
The existing example is needlessly complex.
You have to know that t.Sub returns a Duration
and also have to mentally subtract the two times
to understand what duration should be printed.
Rewrite to focus on just the Duration.String operation.
Change-Id: I00765b6019c07a6ff03022625b556c2b9ba87c09
Reviewed-on: https://go-review.googlesource.com/c/go/+/234893
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
-rw-r--r-- | src/time/example_test.go | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/src/time/example_test.go b/src/time/example_test.go index 15811a62d3..0f9b874944 100644 --- a/src/time/example_test.go +++ b/src/time/example_test.go @@ -50,10 +50,11 @@ func ExampleDuration_Round() { } func ExampleDuration_String() { - t1 := time.Date(2016, time.August, 15, 0, 0, 0, 0, time.UTC) - t2 := time.Date(2017, time.February, 16, 0, 0, 0, 0, time.UTC) - fmt.Println(t2.Sub(t1).String()) - // Output: 4440h0m0s + fmt.Println(1*time.Hour + 2*time.Minute + 300*time.Millisecond) + fmt.Println(300*time.Millisecond) + // Output: + // 1h2m0.3s + // 300ms } func ExampleDuration_Truncate() { |