C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
First result: The first return value from Split is the directory (this excludes the file name at the end).
Second result: This is the file name—the directory is not part of the file name.
Golang program that uses path.Split
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)
}
Output
https://en.wikipedia.org/wiki/
Ubuntu_version_history
Base, Dir: This is the file name at the end. The Dir is the directory at the start.
Golang program that uses path.Base, Dir
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)
}
Output
bird
/home
VolumeName: The VolumeName() func returns the volume name like C—this is the drive letter.
Golang program that uses filepath
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)
}
Output
[RUN ON WINDOWS]
Dir: C:\programs\
File: test.file
Volume: C: