Last active
December 12, 2024 20:10
-
-
Save misterunix/3a041833c733968063bdb04a0a23855b to your computer and use it in GitHub Desktop.
Check if file exists
This file contains hidden or 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( | |
"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