Created
February 4, 2024 13:30
-
-
Save bagasdisini/e60453fe63a0a5c725d99fa01d3dfe53 to your computer and use it in GitHub Desktop.
Golang - Retrieve data from the database continuously.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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