Filepath Package
Fast recall
Quick revision
Core answer
Whether you understand Filepath Package beyond memorized syntax.
Giving a definition without explaining a practical trade-off.
Connect the concept to a small production example and state its complexity or failure mode.
Interview-ready guide
Interview answer
The core functions, executed:
filepath.Join("/usr", "local", "bin")
// "/usr/local/bin" — verified
filepath.Ext("main.go") // ".go" — verified
filepath.Base("/a/b/c.txt") // "c.txt" — verified
filepath.Dir("/a/b/c.txt") // "/a/b"
filepath.Clean("/a/../b//c") // "/b/c"
filepath.Abs("rel/path") // "/current/dir/rel/path"
filepath.Rel("/a", "/a/b/c") // "b/c" — the relative path between two
filepath.Match("*.go", "main.go") // true — glob matchingWhy filepath (not path): filepath uses the OS-native separator (/ on Unix, \ on Windows); the path package is for slash-only URLs. Using filepath means your code works on Windows without changes.
The composition patterns:
Join over string concatenation: "/a" + "/" + "b" works until a component already has a separator — Join handles all cases. It's the ONLY correct way to combine paths.
WalkDir (the modern Walk):
filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() { return nil }
if filepath.Ext(path) == ".go" { fmt.Println(path) }
return nil
})— the find-files pattern: walk a tree, filter by Ext, process.
Filepath Package
Direct Answer
Cross-platform path manipulation: Join (concatenate with correct separators), Base, Dir, Ext, Clean, Abs, Rel, Match, WalkDir — the portable path toolkit.
Deep Dive & Architecture
The separator abstraction, mechanically: filepath.Join uses the OS's Separator constant (\ on Windows); the implementation normalizes any input separators. The same code produces correct paths on both platforms — the package IS the portability layer.
The Clean semantics: removes redundant separators, resolves . and .. lexically (no filesystem access — purely textual). Clean("/a/./b/../c") = "/a/c". The guarantee: the result is the simplest path naming the same file.
The WalkDir vs Walk evolution: Walk takes an fs.FileInfo (requires stat-ing every file — a syscall each); WalkDir takes fs.DirEntry (the info is already available from the directory read — no extra stat). The Go 1.16+ idiom is WalkDir; the API shape is otherwise identical.
The security check: filepath.Clean on user-provided paths before joining to a root (preventing ../ escape) — the traversal-attack defense. The pattern: clean, then verify the result is still under the root.
Common Mistakes & Pitfalls
Giving a definition without explaining a practical trade-off.
Check your understanding
Answer these without looking back at the article.
- When would you use Filepath Package in a real project?
- How would you compare Filepath Package with an alternative?
- What is a common mistake when using Filepath Package?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.