Go言語では、 時間 パッケージは、時刻を確認するだけでなく時刻を表示する機能を提供します。
の 寝る() Go 言語の関数は、最新の Go ルーチンを少なくとも指定された期間停止するために使用されます。 d 。また、スリープ時間が負またはゼロの場合、このメソッドは即座に戻ります。また、この関数は time パッケージの下で定義されます。ここで、これらの機能を使用するには、時間パッケージをインポートする必要があります。
構文:
func Sleep(d Duration)>
ここ、 d 睡眠時間の長さです。
戻り値: 最新のゴルーチンを指定された期間一時停止し、スリープが終了した後に任意の操作の出力を返します。
例 1:
バーティ・ジャ
// 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.....'>)> }> |
>
>
シェルスクリプトのパラメータ
出力:
Sleep Over.....>
ここで、上記のコードを実行した後、main 関数が呼び出されるときに、 寝る メソッドでは、指定された操作が指定された期間停止され、結果が出力されます。
ms wordのクイックアクセスツールバー
例 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'>)> >}> }> |
>
>
ソートのマージ
出力:
output1 timeout....2>
ここで、上記のコードでは、タイムアウト時間 (After() メソッド) がスリープ時間 (Sleep() メソッド) よりも長いため、output1 が出力されます。そのため、出力はタイムアウトが表示される前に出力されますが、その後は以下のケースでは、タイムアウト期間がスリープ時間より短いため、出力を印刷する前にタイムアウトが表示され、したがって timeout….2 が印刷されます。