Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

Tips & Traps

There is no built-in function to check for the existence of a path in Golang. However, you can achieve it using os.Stat + os.IsNotExist.

import "os"
_, err := os.Stat("/some/non-exist/path")
err
stat /some/non-exist/path: no such file or directory
os.IsNotExist(err)
true

The function ExistsPath below is an implementation based the idea abvoe.

func ExistsPath(path string) bool {
	_, err := os.Stat(path)
	if os.IsNotExist(err) {
		return false
	}
	return true
}
ExistsPath("/some/non-exist/path")
false