Skip to content

Instantly share code, notes, and snippets.

@mattes
Last active February 11, 2026 19:47
Show Gist options
  • Select an option

  • Save mattes/d13e273314c3b3ade33f to your computer and use it in GitHub Desktop.

Select an option

Save mattes/d13e273314c3b3ade33f to your computer and use it in GitHub Desktop.
Check if file or directory exists in Golang
if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err) {
// path/to/whatever does not exist
}
if _, err := os.Stat("/path/to/whatever"); !os.IsNotExist(err) {
// path/to/whatever exists
}
@ravindrabhargava
Copy link
Copy Markdown

what about if need to check for "*.txt" in Linux dir if the file exists or not, I tried *.txt but it's not working at all

if _, err := os.Stat("*.txt"); err == nil {
fmt.Printf("File exists\n");
} else {
fmt.Printf("File does not exist\n");
}

@mattes
Copy link
Copy Markdown
Author

mattes commented Sep 21, 2020

@pavelerokhin
Copy link
Copy Markdown

super!

@KEINOS
Copy link
Copy Markdown

KEINOS commented Mar 21, 2021

Great! It was so helpful! 👍

@ondrejsika
Copy link
Copy Markdown

Thanks 👍

@gowizzard
Copy link
Copy Markdown

Thanks

@9cat
Copy link
Copy Markdown

9cat commented Apr 27, 2022

that helps~

@yangyongzhi7
Copy link
Copy Markdown

Thanks

@lukemilby
Copy link
Copy Markdown

I was here.

Thanks

@magicwarms
Copy link
Copy Markdown

Thanks!

@BloodmageThalnos
Copy link
Copy Markdown

shanks!

@stricklandye
Copy link
Copy Markdown

haha, although it is trivial still helps a lot ,thanks!

@mayooot
Copy link
Copy Markdown

mayooot commented Feb 19, 2024

Thanks

@lazypwny751
Copy link
Copy Markdown

thanks 👍

@Anhdao153
Copy link
Copy Markdown

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment