How to check if a file exists in go

Oct 1, 2018 18:02 · 145 words · 1 minute read

Here is a quick snippet to check if a file exists in go

package main

import (
	"fmt"
	"os"
)

func main() {
	if _, err := os.Stat("somefile"); os.IsNotExist(err) {
		fmt.Println("path to \"somefile\" does not exist")
	}
}

In an edge case a file might disappear between an exist check and open

So, it might be a good idea to always open a file and check for returned err.

If the file does not exist, it can be detected by os.IsNotExist(err) and non-existence can be handled there.

A better approach could be use os.OpenFile and try to open or create a file. This would only throw an error if a file already exists.

package main

import (
	"fmt"
	"os"
)

func main() {
	f, err := os.OpenFile("ff", os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
	if err != nil {
		if os.IsExist(err) {
			fmt.Println("file already exists.")
		}

	}
	defer f.Close()
}