Exists
Overview
Exists reports whether path exists.
If you are coming from Node.js, the closest pattern is fs.exists(path).
Signature
go
func Exists(ctx context.Context, path string) (bool, error)Compare: Node.js · Standard Go · gox
js
await fs.promises.access('path');go
_, err := os.Stat(path)
exists := err == nilgo
import "github.com/sahilkhaire/gox/fs"
ok, err := fs.Exists(ctx, "path")Example
go
import "github.com/sahilkhaire/gox/fs"
ok, err := fs.Exists(ctx, "path")Tips
Pass context.Context as the first argument so cancellation and deadlines propagate correctly.
Standard library alternative
Use the standard library directly:
go
_, err := os.Stat(path)
exists := err == nil