Home
Go
path and filepath Examples
Updated Dec 31, 2024
Dot Net Perls
Path. Paths point to things—they lead to files and folders. With the path package in the Go language we can handle paths for web sites.
With filepath, meanwhile, we can parse Windows paths on Windows computers. Filepath supports the native path format for the present computer.
First example. To begin we have a URL from Wikipedia. We can call path.Split to get the file name from the URL. The rest of the path is the directory (the Dir).
Info The first return value from Split is the directory (this excludes the file name at the end).
And The second return value is the file name—the directory is not part of the file name.
package main import ( "fmt" "path" ) func main() { example := "https://en.wikipedia.org/wiki/Ubuntu_version_history" // Split will get the last part after the last slash. // ... This is the file. // ... The dir is the part before the file. dir, file := path.Split(example) fmt.Println(dir) fmt.Println(file) }
https://en.wikipedia.org/wiki/ Ubuntu_version_history
Base, Dir. Sometimes we want more specific path parts in a program. We can use methods like Base() and Dir() to get parts from a path string. The Split() func is not needed.
Detail This is the file name at the end. The Dir is the directory at the start.
package main import ( "fmt" "path" ) func main() { example := "/home/bird" // Base returns the file name after the last slash. file := path.Base(example) fmt.Println(file) // Dir returns the directory without the last file name. dir := path.Dir(example) fmt.Println(dir) }
bird /home
Filepath. Suppose you are running Go on a Windows computer. The filepath module is ideal for parsing paths on Windows—it can handle backslashes.
Detail The VolumeName() func returns the volume name like C—this is the drive letter.
package main import ( "fmt" "path/filepath" ) func main() { fmt.Println("[RUN ON WINDOWS]") // Split into directory and file name. dir, file := filepath.Split("C:\\programs\\test.file") fmt.Println("Dir:", dir) fmt.Println("File:", file) // Get volume (drive letter). volume := filepath.VolumeName("C:\\programs\\test.file") fmt.Println("Volume:", volume) }
[RUN ON WINDOWS] Dir: C:\programs\ File: test.file Volume: C:
PathSeparator. Go can tell us the current platform's path separator. This is found in the "os" module, as it is OS-specific. On Windows and Linux this will return different values.
Tip The PathSeparator is a rune. But we can convert it to a string easily for concatenation or use in string methods.
package main import ( "fmt" "os" ) func main() { fmt.Println(string(os.PathSeparator)) // Use separator in a concatenation. result := "dir" + string(os.PathSeparator) fmt.Println(result) }
/ dir/
Summary. Paths can be parsed directly in Go, with loops and char tests. But the path and filepath packages provide built-in functions that are easier to add.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Dec 31, 2024 (edit).
Home
Changes
© 2007-2025 Sam Allen