How to Schedule Tasks in Go
Tags: schedule, go
Published: 2023-09-19 21:05:47

---
title: How to Schedule Tasks in Go
published: true
date: 2023-09-11 22:13:36 UTC
tags: #schedule #go #golang
canonical_url: https://carlosmv.hashnode.dev/how-to-schedule-tasks-in-go
---

In this article, we are going to learn how to schedule tasks using the schedule package using examples taken from its [documentation](https://github.com/atomicgo/schedule).

As its README page says, this package provides a simple scheduler for Go.

It can run a function at a given time, in a given duration, or repeatedly at a given interval.

```
package main

import (
    "atomicgo.dev/schedule"
    "fmt"
    "time"
)

func main() {
    task := schedule.Every(time.Second, func() bool {
        fmt.Println("1 second is over!")
        return true
    })

    fmt.Println("Some stuff happening...")

    time.Sleep(10 * time.Second)

    task.Stop()
}

```

In the code above we use the schedule package to print in the console the message "1 second is over!" every second. Then this task stops after 10 seconds.

The function `Stop()` stops the scheduler.

We execute this program and we should see the following message in the console:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694294312263/a4ef217b-88fb-4bde-a20d-c99bcb6222b0.png)

```
package main

import (
    "fmt"
    "time"

    "atomicgo.dev/schedule"
)

func main() {
    task := schedule.At(time.Now().Add(5*time.Second), func() {
        fmt.Println("5 seconds are over!")
    })

    fmt.Println("Some stuff happening...")

    task.Wait()
}

```

In this example, the program prints the message "5 seconds are over!" after 5 seconds of the current time.

The `Wait()` method blocks until the scheduler is stopped. `After()` and `At()` will stop automatically after the task is executed.

We execute this program and we should see the following message in the console:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694294375205/572693ff-38ec-488f-9244-835cd3ce13e1.png)

The `After()` function executes a task after a given duration.

```
package main

import (
    "fmt"
    "time"

    "atomicgo.dev/schedule"
)

func main() {
    task := schedule.After(5*time.Second, func() {
        fmt.Println("5 seconds are over!")
    })

    fmt.Println("Some stuff happening...")

    task.Wait()
}

```

In the code above we use the `After()` function to print the message "5 seconds are over!" after 5 seconds are passed.

We execute this program and we should see the following message in the console:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694470242589/8f8533de-6aa7-4cb4-98e6-acec289ce54d.png)

```
package main

import (
    "atomicgo.dev/schedule"
    "fmt"
    "time"
)

func main() {
    task := schedule.At(time.Now().Add(5*time.Second), func() {
        fmt.Println("5 seconds are over!")
    })

    fmt.Println("Some stuff happening...")

    fmt.Println(task.IsActive())
    task.Wait()
    fmt.Println(task.IsActive())
}

```

The `IsActive()` function returns true if a task is executing and false if not.

We execute this program and we should see the following message in the console:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694295591097/39d13549-55be-4b3e-b236-044d12704b72.png)

I think this is a useful function to use when we want to execute a task that depends on whether another task is running or not.

```
package main

import (
    "atomicgo.dev/schedule"
    "fmt"
    "time"
)

func main() {
    task := schedule.At(time.Now().Add(5*time.Second), func() {
        fmt.Println("5 seconds are over!")
    })

    fmt.Println("Some stuff happening...")

    if task.IsActive() == true {
        fmt.Println("There is a task running")
    }
    task.Wait()

}

```

In the code above, we check if a task is running and if is true, the program prints the message "There is a task running" in the console.

We execute this program and we should see the following message in the console:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1694295661549/90028498-2209-4a24-83a6-539f0b508b89.png)

### Conclusion

In this article, we discuss how to use some of the functions in the `schedule` package, like performing tasks at a certain time or tasks repeatedly at a specific interval of time.

This article doesn't cover all the functions and features of the `schedule` package. To know more about you can read its [README page](https://github.com/atomicgo/schedule).

Thank you for taking the time to read this article.

If you have any recommendations about other packages, architectures, how to improve my code, my English, or anything; please leave a comment or contact me through [**Twitter**](https://twitter.com/Carlos_marcv), or [**LinkedIn**](https://www.linkedin.com/in/carlos-marcano-a2135a134/).

### References

[Schedule README](https://github.com/atomicgo/schedule)