Created
July 31, 2023 11:27
-
-
Save alirezaarzehgar/9eb8d3e1e1e8b21ff79f1baf94ead529 to your computer and use it in GitHub Desktop.
I use this snippet for counting my mind ruminations. I think this technique can stop rumination after some times.
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 ( | |
"errors" | |
"fmt" | |
"io/fs" | |
"log" | |
"os" | |
"strconv" | |
) | |
const TMP = "/tmp/rumination.tmp" | |
func main() { | |
if len(os.Args) == 2 && os.Args[1] == "clear" { | |
os.WriteFile(TMP, []byte("0"), fs.ModePerm) | |
fmt.Println("Repeat:", 0) | |
return | |
} | |
var num uint64 = 0 | |
if _, err := os.Stat(TMP); errors.Is(err, os.ErrNotExist) { | |
os.WriteFile(TMP, []byte("0"), fs.ModePerm) | |
} else { | |
var err error | |
data, err := os.ReadFile(TMP) | |
if err != nil { | |
log.Fatal(err) | |
} | |
num, err = strconv.ParseUint(string(data), 10, 64) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
num++ | |
fmt.Println("Repeat:", num) | |
os.WriteFile(TMP, []byte(fmt.Sprint(num)), fs.ModePerm) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment