--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
prog.go:18: invalid operation: time.Since(startTime) * times (mismatched types time.Duration and int64)
On 25 Dec 2015 10:18 a.m., "Tong Sun" <sunto...@gmail.com> wrote:
>
> I.e., it only works with constants. If I replace the " * 15 " with a variable, int or int64, it will error out.
>
> Shall we consider it a bug?
> How can I fix it?
>
In Go both operands to the multiplication operator need to be of the same type.
You can only multiply a time.Duration by a time.Duration.
The reason is works with untyped constants is that untyped constants gain their type from the context they're used in.
Sure, just as you'd expect.You can see in the package documentation that time.Duration is defined astype Duration int64Which means you can do math on them with the builtin operators just like an int64, and you can use any untyped integral constants as time.Duration values (as I did in my example) or convert some integral type you already have (say, an int that you got from len() or whatever) to time.Duration, like time.Duration(value) to use it in a calculation.Depending on the calculations, you might want to convert everything to float64 and then back again to avoid truncation on integer division and integer overflow problems (though avoid it if you can, it's certainly easier to read without the float conversions.)
Thank you Jessta for your further explanation, and Caleb for your code, after which when I read Chris' explanation again, everything make sense to me now.But there is still one thing,
On Thursday, December 24, 2015 at 12:50:25 PM UTC-5, Chris Kastorff wrote:Sure, just as you'd expect.You can see in the package documentation that time.Duration is defined astype Duration int64Which means you can do math on them with the builtin operators just like an int64, and you can use any untyped integral constants as time.Duration values (as I did in my example) or convert some integral type you already have (say, an int that you got from len() or whatever) to time.Duration, like time.Duration(value) to use it in a calculation.Depending on the calculations, you might want to convert everything to float64 and then back again to avoid truncation on integer division and integer overflow problems (though avoid it if you can, it's certainly easier to read without the float conversions.)Please take a lookFor easy of calculation and understanding, It assumes at *each* step of the loop, the elapsed time is 60 seconds (same), and try to estimate the remaining time. Here is the result I run from my local machine:Time taken so far 1m0sFinishing the remaining 80% in 4m0.000008192sFinishing the remaining 60% in 1m0.000002048sFinishing the remaining 40% in 0Finishing the remaining 20% in 0Finishing the remaining 0% in 0This is not what I'm expecting. For e.g., when ii==2, two jobs finished in 60 seconds, so the remaining 3 jobs should finish in 60/2*3=90 seconds, not 1m. And for ii==3, I'm expecting 60/3*2=40 seconds, not 0 seconds.What I'm doing wrong?
https://play.golang.org/p/6bVAXzZKcoI believe they are 32 bit machines so you need int64.