Skip to content

Instantly share code, notes, and snippets.

@misterunix
Last active December 12, 2024 20:10
Show Gist options
  • Save misterunix/3a041833c733968063bdb04a0a23855b to your computer and use it in GitHub Desktop.
Save misterunix/3a041833c733968063bdb04a0a23855b to your computer and use it in GitHub Desktop.
Check if file exists
package main
import(
"fmt"
"os"
)
func main() {
if fileExists("sample.txt") {
fmt.Println("sample file exists")
} else {
fmt.Println("sample file does not exist")
}
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment