To test concurrent goroutines, I added a line to a function to make it take a random time to return (up to one second)
time.Sleep(rand.Int31n(1000) * time.Millisecond)
However when I compiled, I got this error
.\crawler.go:49: invalid operation: rand.Int31n(1000) * time.Millisecond (mismatched types int32 and time.Duration)
Any ideas? How can I multiply a duration?
Rules on How to Multiply Integers. Step 1: Multiply their absolute values. Step 2: Determine the sign of the final answer (in this case it is called the product because we are multiplying) using the following conditions. Condition 1: If the signs of the two numbers are the same, the product is always a positive number. Condition 2: If...
Examples of Integer Multiplications. Example 1: Multiply the integers below. Solution: First, get the absolute value of each number. Next, multiply or find the product of the absolute values. Finally, determine the sign of the final answer. The rule states that if the signs of the two integers are different then the final answer will be negative.
The rules that govern how to multiply and divide integers are very similar. In this lesson, we will focus on the multiplication of integers. Step 1: Multiply their absolute values. Step 2: Determine the sign of the final answer (in this case it is called the product because we are multiplying) using the following conditions.
In Go, you can multiply variables of same type, so you need to have both parts of the expression the same type. The simplest thing you can do is casting an integer to duration before multiplying, but that would violate unit semantics. What would be multiplication of duration by duration in term of units?
int32
and time.Duration
are different types. You need to convert the int32
to a time.Duration
:
time.Sleep(time.Duration(rand.Int31n(1000)) * time.Millisecond)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With