TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to GO

Golang path and filepath Examples (Base, Dir)

Use the path and filepath packages to Split paths apart. Use Dir and Base to get parts of paths.
Path. Paths point to things—they lead to files and folders. With the path package in Golang we can handle paths on web addresses and Linux system.
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).

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. 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.

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
Filepath. Suppose you are running Golang on a Windows computer. The filepath module is ideal for parsing paths on Windows—it can handle backslashes.

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:
A summary. Paths can be parsed directly in Golang, with loops and char tests. But the path and filepath packages provide built-in functions that are easier to add.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf