Multiplying Duration by Integer in Go: Secrets of 2025

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

How to Multiply Duration by Integer in Go? Let's Break It Down with Examples

\n\n

Hello, 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!

\n\n

Why doesn't time.Duration * int work?

\n

Let'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:

\n
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
\n

The 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.

\n\n

Best Practices: How to Multiply Duration by Integer

\n

There are several ways, and we'll look at each one. Choose the one that best suits your task.

\n\n

1. Converting int to time.Duration

\n

The most obvious and safe method is to cast the multiplier to the time.Duration type. Since time.Duration is int64, the conversion works directly:

\n
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
\n

This 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.

\n\n

2. Using time.Duration for Calculations

\n

Sometimes it's more convenient to work directly with time.Duration constants. For example, if you need to increase an interval several times:

\n
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
\n

This 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.

\n\n

3. Wrapper Function for Convenience

\n

If multiplying duration by int occurs frequently in your code, create a small function. This will improve readability and avoid duplication:

\n
package 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
\n

In 2025, when projects become more complex, such helper functions are a must-have. They protect against errors and make the code self-documenting.

\n\n

4. Floating-Point Multiplication (Advanced Level)

\n

Sometimes 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:

\n
package main\n\nimport (\n    "fmt"\n    "time"\n)\n\nfunc main() {\n    d := 100 * time.Millisecond\n    factor := 1.5\n    result :=                

Blogs

Book Recommendations