This is a creation in Article, where the information may have evolved or changed.
encounter a Golang time. Sleep () problem, this Golang time.sleep function seems to be finer than Python ruby, python wait is only Time.sleep (), and Golang can time. Sleep (Ten * time. Second) milliseconds, seconds and other different dates to engage in ... The big thing is not dry, the net whole is useless ...
The article wrote a little messy, welcome to spray! In addition, the article continues to update, please go to the original address to view the update http://xiaorui.cc/?p=3034
Reproduce the problem, use math/rannd to get a random number within 10, and then Time.sleep () wait ...
Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">num: = Rand. INT31N (time.sleep) (num * time). Second)</textarea>
123 |
Num : = rand. int31n (ten) Time . Sleep (num * time. ) Second ) |
You encounter the following problem:
Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">#xiaorui. cc# command-line-arguments./lock.go:88:invalid operation:int (defaulttimeout) * time. Second (mismatched types int and time. Duration)</textarea>
1234 |
#xiaorui. CC# command-line-arguments./Lock.Go: the: InvalidOperation: int(defaulttimeout) * Time.Second (mismatchedTypes int and Time.Duration) |
The workaround:
Python <textarea wrap= "soft" class= "Crayon-plain print-no" data-settings= "readonly style="-MOZ-TAB-SIZE:4; -o-tab-size:4; -webkit-tab-size:4; Tab-size:4; FONT-SIZE:12PX!important; line-height:15px!important; " >time. Sleep (time. Duration (NUM) * time. Second) </textarea>
12 |
Time . Sleep (time. Duration (num) * time. Second ) |
The reason for the beginning of the period is that Rand has a random number problem, simply look at Rand's function description feel no problem! Here are the reasons for this:
Python <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">func sleep (d Duration) sleep pauses the current goroutine for at least the Duration D. A negative or zero duration causes Sleep to return immediately.</textarea>
123 |
funcSleep(D Duration) Sleeppauses the CurrentGoroutine for atleast theDuration D. A Negativeor ZeroDurationcausesSleep toreturn immediately. |
Int32 and Time. Duration is different types. You need to convert the int32 to a time. Duration, such as time. Sleep (time. Duration (Rand. INT31N (+)) * Time.millisecond).
The following is an example of a complete Golang random number and then Time.sleep ():
Python Package <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">Mainimport ("FMT" "Math/rand" "Time") Func main () {rand. Seed (time. Now (). Unixnano ()) for I: = 0; I < 10; i++ {x: = rand. INTN (Ten) fmt. PRINTLN (x) time. Sleep (time. Duration (x) * time. Second)}}</textarea>
1234567891011121314151617 |
PackageMainImport ( "FMT" "Math/rand" "Time")funcMain() { Rand.Seed( Time. Now().Unixnano()) for I := 0; I < Ten; I++ { x := Rand.INTN(Ten) FMT.Println(x) Time.Sleep( Time.Duration(x) * Time.Second) }} |
Although the time.sleep of Golang can be directly used as a number, it cannot be float floating point type.
Python Time <textarea wrap="soft" class="crayon-plain print-no" data-settings="" readonly="" style="-moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4; font-size: 12px !important; line-height: 15px !important;">. Sleep (1 * time. Second)//Can be time. Sleep (1.1 * time. Second)//bugtime.sleep (time. Duration (yourtime) * time. Second)//Can</textarea>
1234 |
Time.Sleep(1 * Time.Second) //OK Time.Sleep(1.1 * Time.Second) //BUG Time.Sleep( Time.Duration(Yourtime) * Time.Second) //OK |
https://golang.org/pkg /time/#Duration