How to multiply duration by an integer in Go?
Hello everyone! I recently started learning Go and encountered a problem when working with time intervals. I need to perform a repeated action with a delay that increases by a multiple. For example, I want to make a pause of 1 second, then 2 seconds, then 3, and so on.\n\nI wrote this code:\n\n```go\npackage main\n\nimport (\n "fmt"\n "time"\n)\n\nfunc main() {\n baseDelay := 1 * time.Second\n multiplier := 3\n totalDelay := baseDelay * multiplier // Error!\n fmt.Println("Total delay:", totalDelay)\n}\n```\n\nBut the compiler gives an error: `invalid operation: baseDelay * multiplier (mismatched types time.Duration and int)`. I understand that the types are different, but how do I correctly multiply a duration by an integer? In other languages (e.g., Python) this works directly.
No answers yet.
Log in to leave an answer