Recently, while working on a post about Kubernetes Operators, I faced an issue with “time multiplication” and couldn’t understand why Go did what it did. If you have some background with Kubernetes Operators, I recommend checking out the issue itself. If you are focussed on just Go, here’s the code:

package main

import (
	"fmt"
	"time"
)

func main() {
	d := 5 * time.Second
	fmt.Println(d)
	fmt.Println(d * time.Second)
}

and here’s the output:

$ go run ./main.go
5s
1388888h53m20s

I expected the second fmt.Println output to print 5s as well. 1388888h53m20s made no sense to me! I ended up pinging on the Gophers Slack and got an answer which surprised me.

The reason for this output is how time.Second is declared in the standard library’s time package - it’s an integer with the value 10^9. So the expression inside second fmt.Println would translate to 5 * 10^9 * 10^9 and the result of that would be printed by the String method.

That’s it Link to heading

I had a hunch while posting the GitHub issue on operator-sdk repository, but I couldn’t think of a possible cause till I saw the first response to my query. That was a pretty neat thing I recently learned about Go.