Avatar ·

How to check if a file or directory exists in Go?

Hi everyone! I just started learning Go and encountered a seemingly simple task: I need to write a function that checks whether a file or folder exists at a given path. I'm used to Python, where there is `os.path.exists()`, but in Go I can't find such a simple way. I tried using `os.Stat()`, but I don't fully understand how to handle errors correctly. For example, I wrote this code: ```go package main import ( "fmt" "os" ) func main() { path := "./test.txt" _, err := os.Stat(path) if err == nil { fmt.Println("File exists") } else if os.IsNotExist(err) { fmt.Println("File does not exist") } else { fmt.Println("An error occurred:", err) } } ``` But I'm confused that `os.Stat()` returns an er