Last active
December 1, 2023 07:07
-
-
Save WahidinAji/a6d5f7b12dc4a9b293e99a1399a847fe to your computer and use it in GitHub Desktop.
no description
This file contains 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" | |
) | |
func main() { | |
//make a program standby | |
times := make(chan time.Time) | |
go doJob(times) | |
for { | |
//receive time from timer | |
t := <-times | |
fmt.Println("the time is", t) | |
} | |
} | |
var count int | |
var second int | |
var minute int | |
// make job with every thirty seconds | |
func doJob(times chan time.Time) { | |
go seconds() | |
for { | |
times <- time.Now() | |
//send how many times timer has been called | |
count++ | |
fmt.Println("timer has been called", count, "times") | |
fmt.Println("executing job...") | |
time.Sleep(30 * time.Second) | |
} | |
} | |
// count every single second | |
func seconds() { | |
for { | |
second++ | |
fmt.Printf("s: %d\n", second) | |
//reset every 60 seconds | |
if second == 60 { | |
second = 0 | |
go minutes() | |
} | |
time.Sleep(1 * time.Second) | |
} | |
} | |
// count minutes | |
func minutes() { | |
minute++ | |
fmt.Printf("m: %d\n", minute) | |
//reset every 60 minutes | |
if minute == 60 { | |
minute = 0 | |
} | |
time.Sleep(1 * time.Minute) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment