Skip to content

Instantly share code, notes, and snippets.

@shanenoi
Created March 23, 2023 06:53
Show Gist options
  • Save shanenoi/e67d9abe1d837c42f5c8da8e179b0c39 to your computer and use it in GitHub Desktop.
Save shanenoi/e67d9abe1d837c42f5c8da8e179b0c39 to your computer and use it in GitHub Desktop.
utils
func LogTmpFile(f func(pipe io.Writer)) {
randomWithSeed := func(seed int64) int {
rand.Seed(seed)
return rand.Intn(100)
}
dir := "/tmp/lll"
err := os.MkdirAll(dir, 0755)
if err != nil {
log.Printf("Error creating directory: %v\n", err)
panic(err)
}
now := time.Now().Unix()
filename := fmt.Sprintf("%s/%d.%d", dir, now, randomWithSeed(now))
pipe, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
fmt.Printf("Error opening file: %v\n", err)
panic(err)
}
defer func() {
if err := pipe.Close(); err != nil {
log.Println("Error closing file: ", err)
panic(err)
}
}()
f(pipe)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment