Go Development seeks great God help

Source: Internet
Author: User

Go how to implement deferred task execution after the main function to exit the   in the main function is a select{} can be implemented after execution in the exit, but the actual project application will certainly not do so, such as the execution of a task every 20 seconds to do what should be done

Func Main () {//go timer1 ()//timer2 () for I: =1; i <10; i++ {task: =&task{i+1, "a", ""} task. Start ()}}type Task struct {delay int key string data string}func NewTask (delay Int,key, data string) *task     {task: =&task{delay:delay, Key:key, Data:data,} return Task}func (Task * Task) Start () { Go Task.run ()}func (Task * Task) run () {FMT. Println ("Start Execution") Second: =time. Duration (task.delay) Timer: =time. Newticker (Second * time. Second)//val:=make (Chan string) for {select {case <-timer. C:/*fmt. Println ("Minus Before:" +string (Task.delay)) if (Task.delay >0) {task.delay--fmt. Println (Task.delay)} if (Task.delay = = 0) {fmt. Println ("Time to execute callback") HttpPost (task.data) timer. Stop ()}*/FMT. Println ("Time to execute callback") HttpPost (task.data) timer. Stop ()}}}
181 reads  

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.