Home > Article > Backend Development > Why Does `time.Sleep(i * time.Millisecond)` Not Compile in Go?
In Go, the multiplication operator requires operands of the same type, or an untyped constant and a non-constant operand.
In this example, time.Millisecond is of type time.Duration, and 1000 is an untyped integer constant. The multiplication operator converts 1000 to a time.Duration to match the type of time.Millisecond.
However, the following code will not compile:
In this case, both operands are of type int, which are not identical to time.Duration. To resolve this issue, you can convert i to a time.Duration before using it in the multiplication:
The above is the detailed content of Why Does `time.Sleep(i * time.Millisecond)` Not Compile in Go?. For more information, please follow other related articles on the PHP Chinese website!