Go 언어에서는 시간 패키지는 시청 시간뿐만 아니라 결정하는 기능을 제공합니다.
그만큼 잠() Go 언어의 함수는 적어도 명시된 기간 동안 최신 go-routine을 중지하는 데 사용됩니다. 디 . 그리고 수면 기간이 음수이거나 0이면 이 메서드가 즉시 반환됩니다. 또한 이 함수는 time 패키지에 정의되어 있습니다. 여기서 이러한 기능을 사용하려면 time 패키지를 가져와야 합니다.
통사론:
func Sleep(d Duration)>
여기, 디 잠자는 시간이다.
반환 값: 명시된 기간 동안 최신 go-routine을 일시 중지한 다음 절전 모드가 끝나면 모든 작업의 출력을 반환합니다.
예시 1:
CSS 배경
js 세트
// 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 함수가 호출될 때 위의 코드를 실행한 후 잠 메소드는 지정된 작업이 지정된 기간 동안 중지된 후 결과가 인쇄됩니다.
예시 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>
여기서 위의 코드에서는 출력1이 타임아웃 기간(After() 메소드)이 슬립 시간(Sleep() 메소드)보다 길어서 출력되므로 타임아웃이 표시되기 전에 출력이 인쇄되지만 그 이후에는 아래의 경우 타임아웃 기간이 슬립 시간보다 짧으므로 출력을 인쇄하기 전에 타임아웃이 표시되므로 timeout….2가 인쇄됩니다.