Sobes.tech
Junior — Middle

Can you explain the polling mechanism in Go?

sobes.tech AI

Answer from AI

Polling in Go is a mechanism for periodically checking the status of a resource or event. Instead of waiting for a notification, the program regularly polls the data source at a set interval.

In Go, this is often implemented using a loop and the time.Tick or time.Sleep functions to create delays between checks.

Example of simple polling:

package main

import (
    "fmt"
    "time"
)

func main() {
    ticker := time.NewTicker(2 * time.Second)
    defer ticker.Stop()

    for {
        select {
        case <-ticker.C:
            fmt.Println("Checking status...")
            // Here you can add your check logic
        }
    }
}

This approach is useful when you need to regularly check for updates, such as the status of a file, server response, or message queue.

Can you explain the polling mechanism in Go? — Golang - sobes.tech