How to check if a file or directory exists using the os package in Golang?

To check if a file or directory exists using the os package in Golang, you can use the os.Stat() function. Here's an example:

package main import ( "fmt" "os" ) func main() { // File path or directory path to check filePath := "/path/to/file.txt" // Check if the file/directory exists if _, err := os.Stat(filePath); os.IsNotExist(err) { fmt.Println("File or directory does not exist") } else { fmt.Println("File or directory exists") } }

Make sure to replace /path/to/file.txt with the actual file path or directory path that you want to check. The os.Stat() function returns a FileInfo object and an error. If the error returned by os.Stat() is of type os.ErrNotExist or its equivalent, it means the file or directory does not exist.