I know I could check the file does exist or not in Golang by the answers of the following questions.
- How to check whether a file or directory denoted by a path exists in Golang?
- How to check if a file exists in Go?
- Gist - Check if file or directory exists in Golang
The code looks like this.
_, err := os.Stat(path)
if err == nil {
log.Printf("File %s exists.", path)
} else if os.IsNotExist(err) {
log.Printf("File %s not exists.", path)
} else {
log.Printf("File %s stat error: %v", path, err)
}
But here's my real question, how do I check the filename does exist (has been used) in the specified directory? For example if I have a file tree like this:
--- uploads
|- foo.png
|- bar.mp4
I wanted to check if there's any file is using the specified name..
used := filenameUsed("uploads/foo")
fmt.Println(used) // Output: true
used = filenameUsed("uploads/hello")
fmt.Println(used) // Output: false
How do I implement the filenameUsed
function?
Google gave me a path/filepath
package as the result but I have no clue about how to use it.