Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
February 19, 2022 04:06 pm GMT

Manage time specified operations with Go in easy way.

In programming we need specifying time for our code. Sometimes we wait until our crush end her relationship. Sometimes we wait until summer to change our job.

Let's Start!

For doing thing with specified time I will show the basics in Go. In the first part we will look 2 simple code part. After we will discuss about how can we improve them and make it usable even production environment.

Part 1 Basics Of Time Specifying in Go

For time specifying we will use 2 basic technique. First one is for blocking current goroutine until specified time.

In this example goroutine blocked for 5 second at 3rd line and continues after this. If you want waiting more time and doing more readable. You can use time.Until(). Here a basic example for this.

Second technique is blocking goroutine forever and make it cron engine. For doing this we have an excellent function called time.Tick(). This method takes duration and returns channel that triggers at every duration loop.

If you dont care about current time and just want to execute code continuously you can use this one. Also this way is more manageable and allow control flow with context or specific trigger channels.

Part 2 Lets Improve Them With Context Package

Context package is so useful. Because Context gives you a power of cancellation. You can cancel functions with it. If you are doing things with time, context is like Thors Mjlnir for you.

Key part of using context is based on for { select{} } loop. Also you must use async for be able to use context. Because If your code flow dont run, you cant cancel the context.

Lets do some examples. First we will implement a basic wait function with context. If context cancel called before timer end, function will exit without wait to timer end. In that case our program just wait 5 second instead of 5 minutes. That can annoy your goroutine :)

For key point. We split our waiting routine and main routine. We must do that because if we didnt do that we cant call cancel function asynchronously.

Creating cron engine with context is basic than previous example. Just add context.Done() to the select statement.

For a final example I want to add more complex, beautiful and useful cron engine example from here. I wont add code to article. You can use link for this. Also this repository is a great implementation and my inspiration for writing this article.

I would like to hear all feedbacks about this article. Please feel free to give feedback. I want to hear all of good and bad feedbacks :)

Im planning to expand this article some. If you have an idea what can I talk about at the feature of this article, I want to hear it.

Inspired Articles

Good Repositories


Original Link: https://dev.to/go/manage-time-specified-operations-with-go-in-easy-way-abc

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To