Golang's wait.until simple test case

Source: Internet
Author: User

For testing purposes, verify wait. The use of until

//测试wait.Until() 的用途package mainimport (    "fmt"    "k8s.io/apimachinery/pkg/util/wait"    "time")type stop struct {}func main() {    stopCh := make(chan struct{})    //初始化一个计数器    i := 0    go wait.Until(func() {        fmt.Printf("----%d----\n", i)        i++    }, time.Second, stopCh)    time.Sleep(time.Second * 10)    stopCh <- stop{}    // 下面的形式,也是可以的    //stopCh < struct {    //}{}    fmt.Println("---上面的go routines 结束----")    // 主程序,再休息3秒钟,再结束    time.Sleep(time.Second * 3)    fmt.Println("---主程序结束----")}

Summarize:

就是启动一个协程,每隔一定的时间,就去运行声明的匿名函数,直到接收到结束信号 就关闭这个协程

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.