En l'idioma Go, temps paquets ofereix la funcionalitat per determinar així com el temps de visualització.
El Dormir () La funció en llenguatge Go s'utilitza per aturar la darrera rutina de go durant almenys la durada indicada d . I una durada negativa o zero del son farà que aquest mètode torni a l'instant. A més, aquesta funció es defineix sota el paquet de temps. Aquí, heu d'importar el paquet de temps per utilitzar aquestes funcions.
Sintaxi:
func Sleep(d Duration)>
Aquí, d és la durada del temps per dormir.
Valor de retorn: Posa en pausa l'última rutina d'execució durant la durada indicada i després retorna la sortida de qualsevol operació un cop finalitzat el son.
Exemple 1:
per a cada mecanografia
// Golang program to illustrate the usage of> // Sleep() function> > // Including main package> package main> > // Importing fmt and time> import (> >'fmt'> >'time'> )> > // Main function> func main() {> > >// Calling Sleep method> >time>.Sleep(8 *>time>.Second)> > >// Printed after sleep is over> >fmt.Println(>'Sleep Over.....'>)> }> |
constructors en java
>
>
Sortida:
inseriu una marca d'aigua a Word
Sleep Over.....>
Aquí, després d'executar el codi anterior, quan es crida la funció principal a causa de Dormir Mètode l'operació indicada s'atura durant el temps donat i després s'imprimeix el resultat.
Exemple 2:
// Golang program to illustrate the usage of> // Sleep() function> > // Including main package> package main> > // Importing time and fmt> import (> >'fmt'> >'time'> )> > // Main function> func main() {> > >// Creating channel using> >// make keyword> >mychan1 := make(chan string, 2)> > >// Calling Sleep function of go> >go func() {> >time>.Sleep(2 *>time>.Second)> > >// Displayed after sleep overs> >mychan1 <->'output1'> >}()> > >// Select statement> >select {> > >// Case statement> >case> out := <-mychan1:> >fmt.Println(out)> > >// Calling After method> >case> <->time>.After(3 *>time>.Second):> >fmt.Println(>'timeout....1'>)> >}> > >// Again Creating channel using> >// make keyword> >mychan2 := make(chan string, 2)> > >// Calling Sleep method of go> >go func() {> >time>.Sleep(6 *>time>.Second)> > >// Printed after sleep overs> >mychan2 <->'output2'> >}()> > >// Select statement> >select {> > >// Case statement> >case> out := <-mychan2:> >fmt.Println(out)> > >// Calling After method> >case> <->time>.After(3 *>time>.Second):> >fmt.Println(>'timeout....2'>)> >}> }> |
>
>
edat de shilpa shetty
Sortida:
output1 timeout....2>
Aquí, al codi anterior, output1 s'imprimeix ja que la durada del temps d'espera (al mètode After()) és més gran que el temps d'espera (al mètode Sleep()), de manera que la sortida s'imprimeix abans que es mostri el temps d'espera, però després d'això, el La durada del temps d'espera és inferior al temps d'espera, de manera que abans d'imprimir la sortida es mostra el temps d'espera, per tant, s'imprimeix el temps d'espera...2.