filepath.Join, filepath.Split, filepath.Base, filepath.Dir, filepath.Ext, filepath.Clean
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) stringBuild, split, and lexically clean operating-system paths. On this darwin host the separator is a slash.
| Parameter | Description |
|---|---|
elem | Path elements to join. Empty elements are ignored. If every element is empty, Join returns the empty string. |
path | The path to split, inspect, or clean. |
Returns: Join, Base, Dir, Ext, and Clean return strings. Split returns dir and file such that path == dir+file.
- These outputs were measured on darwin/arm64. Do not treat them as Windows results.
- Base of an empty path is ".", and Dir of an empty path is ".". Ext is empty when the final element has no dot.
package main
import (
"fmt"
"path/filepath"
)
func main() {
fmt.Printf("separator=%q\n", string(filepath.Separator))
fmt.Println(filepath.Join("home", "ada", "src.go"))
fmt.Println(filepath.Join("a", "/b"))
fmt.Println(filepath.Join("a", "", "b"))
dir, file := filepath.Split("home/ada/src.go")
fmt.Printf("%q %q\n", dir, file)
fmt.Println(filepath.Base("home/ada/src.go"))
fmt.Println(filepath.Dir("home/ada/src.go"))
fmt.Println(filepath.Ext("src.go"))
fmt.Println(filepath.Clean("a/b/../c//./d"))
}Expected output
separator="/"
home/ada/src.go
a/b
a/b
"home/ada/" "src.go"
src.go
home/ada
.go
a/c/d