Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to multiply duration by integer?

Tags:

time

go

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?

like image 454
Colonel Panic Avatar asked Jul 10 '13 14:07

Colonel Panic


People also ask

What are the rules for multiplying integers?

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

What is an example of an integer multiplication?

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.

How do you multiply and divide integers?

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.

How do you multiply variables of the same type in go?

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?


1 Answers

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) 
like image 148
mna Avatar answered Sep 29 '22 19:09

mna