49 lines
913 B
Go
49 lines
913 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
"sync"
|
|
)
|
|
|
|
const filename string = "countdown.txt"
|
|
var until time.Time = time.Now().Add(time.Minute)
|
|
|
|
func main() {
|
|
d, err := time.ParseDuration("1s")
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
os.Exit(1)
|
|
}
|
|
|
|
fmt.Printf("until: %s\n", until)
|
|
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(1)
|
|
ticker := time.NewTicker(d)
|
|
go func(c <-chan time.Time) {
|
|
fmt.Println("XX")
|
|
_ = <-c
|
|
count := time.Until(until)
|
|
for ; count >= 0; count = time.Until(until) {
|
|
_ = <-c
|
|
str := fmt.Sprintf("%02.0f:%02.0f:%02.0f\n", count.Hours(), count.Minutes(), count.Seconds())
|
|
err = os.WriteFile(filename, []byte(str), 0777)
|
|
if err != nil {
|
|
fmt.Println("unable to write file: %v\n", err)
|
|
}
|
|
}
|
|
wg.Done()
|
|
}(ticker.C)
|
|
|
|
wg.Wait()
|
|
|
|
err = os.WriteFile(filename, []byte("00:00:00"), 0777)
|
|
if err != nil {
|
|
fmt.Println("unable to write file: %v\n", err)
|
|
}
|
|
|
|
fmt.Println("\nDONE")
|
|
}
|