Skip to content
Tutorials Center
My learning

Topics

Go Reference ยท Errors, I/O and Files

Go path/filepath package

Reference for Go path/filepath: Join, Base, Dir, Ext, Clean, Abs, Rel, Split, Match, Glob and EvalSymlinks, with OS-specific separators explained.

The path/filepath package manipulates file names the way the current operating system writes them: / separators on Linux and macOS, \ and drive letters on Windows. It works purely on strings (except Abs, Glob, EvalSymlinks and WalkDir, which touch the file system), so it never checks that a path exists.

Use filepath for paths on disk. Use the sibling package path for slash-separated paths that are not file names, such as URL paths and the names inside io/fs file systems or embed.

Go
p := filepath.Join("data", "reports", "2026", "q1.csv")
// data/reports/2026/q1.csv   (data\reports\2026\q1.csv on Windows)

Try it

Go Taking paths apart and putting them together

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Go Match and Glob

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with bufio.Scanner or fmt.Scan), it gets the next line from this box.

Syntax

Go
func Join(elem ...string) string
func Split(path string) (dir, file string)
func Base(path string) string
func Dir(path string) string
func Ext(path string) string
func Clean(path string) string
func Abs(path string) (string, error)
func Rel(basepath, targpath string) (string, error)
func IsAbs(path string) bool
func IsLocal(path string) bool
func Match(pattern, name string) (matched bool, err error)
func Glob(pattern string) (matches []string, err error)
func EvalSymlinks(path string) (string, error)
func WalkDir(root string, fn fs.WalkDirFunc) error
func Localize(path string) (string, error)
func VolumeName(path string) string
func SplitList(path string) []string
func ToSlash(path string) string
func FromSlash(path string) string
const Separator = os.PathSeparator
Function Behavior
Join Joins with the OS separator, skips empty elements and cleans the result.
Base Last element; "" gives ".", trailing separators are removed.
Dir Everything but the last element, cleaned; no directory gives ".".
Ext The suffix starting at the last dot in the final element (including the dot), or "".
Clean Removes ., resolves .. lexically, collapses repeated separators.
Rel Relative path from basepath to targpath; error if impossible (different volumes or an unresolvable ..).
Match Shell-style patterns: * (no separators), ?, [a-z], [^x], and a backslash to escape (not on Windows). Returns ErrBadPattern for malformed patterns.
Glob Files matching the pattern; ignores I/O errors, returns ErrBadPattern only for bad syntax. There is no **.
IsLocal Reports whether a path stays inside the current directory (Go 1.20); useful for validating untrusted names.

Usage notes

  • Never build paths with + or fmt.Sprintf("%s/%s"); use Join, which handles separators and ...
  • Clean is lexical. a/../b becomes b even if a is a symbolic link elsewhere. Use EvalSymlinks to resolve real links.
  • Ext returns only the last extension: archive.tar.gz gives .gz.
  • Path traversal: joining an untrusted name such as ../../etc/passwd onto a base directory escapes it. Validate with filepath.IsLocal, or use os.Root (Go 1.24) so the OS enforces the boundary.
  • Walk is older than WalkDir and slower, because it calls Lstat on every file. Prefer WalkDir.
  • Glob and Match use the separator of the OS, so on Windows the backslash cannot be used as an escape character.
  • Slash-separated paths: filepath.ToSlash and FromSlash convert between forms, for example when reading names from embed or fs.FS.
  • Symlink-aware size or type checks need os.Lstat; filepath itself does not look at the file system for most functions.