Skip to content

Instantly share code, notes, and snippets.

@bagasdisini
Created February 4, 2024 13:30
Show Gist options
  • Save bagasdisini/e60453fe63a0a5c725d99fa01d3dfe53 to your computer and use it in GitHub Desktop.
Save bagasdisini/e60453fe63a0a5c725d99fa01d3dfe53 to your computer and use it in GitHub Desktop.
Golang - Retrieve data from the database continuously.
package main
import (
"fmt"
"time"
)
var data string
func main() {
// Start a goroutine to periodically retrieve data
go func() {
for {
newData := retrieveData()
if newData != data {
data = newData
fmt.Println("Data updated:", data)
}
// 5 second delay
time.Sleep(time.Second * 5)
}
}()
// Wait forever
select {}
}
func retrieveData() string {
// Retrieve data from database or other source
// ...
return "retrieved data"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment