Home  >  Article  >  Backend Development  >  Why Does `time.Sleep(i * time.Millisecond)` Not Compile in Go?

Why Does `time.Sleep(i * time.Millisecond)` Not Compile in Go?

Barbara Streisand
Barbara StreisandOriginal
2024-11-10 05:16:02285browse

Why Does `time.Sleep(i * time.Millisecond)` Not Compile in Go?

time.Millisecond * int Confusion

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn