How to Multiply Duration by Integer in Go? Let's Break It Down with Examples
\n\nHello, colleague! If you've ever written Go code that works with timers, intervals, or just delays, you've probably encountered this question. You take time.Duration, want to multiply it by an integer — and you get a compilation error. Sound familiar? Don't worry, this is one of the most popular questions on Stack Overflow with thousands of views. In this article, we'll break down why this happens and how to correctly solve the problem in 2025. Let's go!
Why doesn't time.Duration * int work?
\nLet's get straight to the point. In Go, time.Duration is not just a number, but a separate type based on int64 (nanoseconds). When you write:
package main\n\nimport (\n "fmt"\n "time"\n)\n\nfunc main() {\n d := 2 * time.Second\n multiplier := 3\n result := d * multiplier // Compilation error!\n fmt.Println(result)\n}\n\nThe compiler will output something like: invalid operation: d * multiplier (mismatched types time.Duration and int). Go has strict typing, and you cannot directly multiply different types. This is protection against silly mistakes, but sometimes it's annoying. In 2025, this rule hasn't gone away, and you need to work around it properly.
Best Practices: How to Multiply Duration by Integer
\nThere are several ways, and we'll look at each one. Choose the one that best suits your task.
\n\n1. Converting int to time.Duration
\nThe most obvious and safe method is to cast the multiplier to the time.Duration type. Since time.Duration is int64, the conversion works directly:
package main\n\nimport (\n "fmt"\n "time"\n)\n\nfunc main() {\n d := 2 * time.Second\n multiplier := 3\n result := d * time.Duration(multiplier)\n fmt.Println(result) // Output: 6s\n}\n\nThis method is ideal when the multiplier is a small positive number. But be careful: if multiplier is negative or very large, the result may be unexpected (overflow). In 2025, this is still relevant, so check boundary values.
2. Using time.Duration for Calculations
\nSometimes it's more convenient to work directly with time.Duration constants. For example, if you need to increase an interval several times:
package main\n\nimport (\n "fmt"\n "time"\n)\n\nfunc main() {\n base := 500 * time.Millisecond\n multiplier := time.Duration(4)\n result := base * multiplier\n fmt.Println(result) // Output: 2s\n}\n\nThis approach is easier to read, especially if the multiplier is a constant. But don't forget that time.Duration is nanoseconds, so time.Duration(4) is 4 nanoseconds, not 4 time units. For multiplying by "4 times" this works, but for other purposes it doesn't.
3. Wrapper Function for Convenience
\nIf multiplying duration by int occurs frequently in your code, create a small function. This will improve readability and avoid duplication:
\npackage main\n\nimport (\n "fmt"\n "time"\n)\n\nfunc multiplyDuration(d time.Duration, factor int) time.Duration {\n return d * time.Duration(factor)\n}\n\nfunc main() {\n d := 3 * time.Minute\n result := multiplyDuration(d, 2)\n fmt.Println(result) // Output: 6m0s\n}\n\nIn 2025, when projects become more complex, such helper functions are a must-have. They protect against errors and make the code self-documenting.
\n\n4. Floating-Point Multiplication (Advanced Level)
\nSometimes you need to multiply a duration by a fractional number (e.g., 1.5). Go does not allow multiplying time.Duration by float64, but you can cheat:
package main\n\nimport (\n "fmt"\n "time"\n)\n\nfunc main() {\n d := 100 * time.Millisecond\n factor := 1.5\n result :=