Answers for "golang check if file exists"

1

check if file exists golang

if _, err := os.Stat("/path/to/whatever"); os.IsNotExist(err) {
  // path/to/whatever does not exist
}
Posted by: Guest on November-05-2020
0

go if file exists

import "os"

func main() {
	if functs.FileExists("path/to/file") {
		fmt.Println("Example file found.")
	} else {
		fmt.Println("Example file not found!")
	}
}

func FileExists(filename string) bool {
	info, err := os.Stat(filename)
	if os.IsNotExist(err) {
		return false
	}
	return !info.IsDir()
}
Posted by: Guest on July-28-2020
0

golang check if file exists

file, err := os.OpenFile(...)
if errors.Is(err, os.ErrNotExist) {
    // handle the case where the file doesn't exist
}
Posted by: Guest on October-10-2021

Code answers related to "golang check if file exists"

Browse Popular Code Answers by Language