random file

This commit is contained in:
andrzej 2024-10-27 01:48:00 +02:00
parent bb3a2d2de3
commit 0655e6a1ee
1 changed files with 16 additions and 0 deletions

16
files.go Normal file
View File

@ -0,0 +1,16 @@
package main
import (
"math/rand"
"os"
)
func getRandomFile(dir string) (string, error) {
files, err := os.ReadDir(dir)
if err != nil {
return "", err
}
randomIndex := rand.Intn(len(files))
randomImg := files[randomIndex]
return dir + "/" + randomImg.Name(), nil
}