Golang program to get the name of the file from the absolute path


We are going to use the filepath and string functions of golang to get the file name from the absolute path. An absolute directory begins with the root directory and includes all intermediate directories. In the first method, we will use filepath package functions and in the second example, we will use strings package functions.

Method 1: Using filepath Package

The base name of the file, which is the file name without the directory path, is extracted in this program using the filepath.Base function from the path/filepath package. The name of the extracted file is then displayed on the console.

Syntax

filepath.Base()

Use the filepath.Base function from the path/filepath package to determine the file path’s base name. The last part of the file path, excluding the directory and any separators, is referred to as the base name.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package), path/filepath package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function create a file_path variable and assign the path to the file_path.

  • Step 3 − Use filepath.Base function to get the name of the file from the path assigned into the variable above.

  • Step 4 − Print the filename on the console using fmt.Println() function where ln means new line.

Example

In this example we will use filepath.Base() function to get the file name

package main
import (
   "fmt"
   "path/filepath"
)

func main() { 
   file_path := "/path/to/myfile.txt" //assign the absolute path
   file_name := filepath.Base(file_path)  //use this built-in function to obtain filename
   fmt.Println(" The file Name from the absolute path is:", file_name)
}

Output

The file Name from the absolute path is: myfile.txt

Method 2: Using strings.TrimRight and strings.Split Function

The strings.TrimRight function is used in this program to remove any trailing slashes in the file path. The file path is divided into distinct parts and separated from one another by slashes using the Split function. The final element of the split file path is then accessed to retrieve the file name.

Syntax

func Split(str, sep string) []string

Split() function is used to split a string through a provided separator. This function is present in strings package and it accepts the string to split as an argument along with a separator. The function then returns the final array of strings as a result.

strings.TrimRight

Use the TrimRight function to delete a string's specified characters as well as any trailing spaces.

Algorithm

  • Step 1 − Create a package main and declare fmt(format package), strings package in the program where main produces executable codes and fmt helps in formatting input and output.

  • Step 2 − Create a main function and in that function create a variable file and assign it the absolute path.

  • Step 3 − In this Step, use TrimRight function on the file variable to remove any trailing slashes with “/” tis as an input.

  • Step 4 − Use the Split function to get the last component of the file.

  • Step 5 − Print the file name on the console.

  • Step 6 − The print statement is executed using fmt.Println() function where ln means new line.

Example

In this example, we will use strings.Split and strings.TrimRight function to execute the program.

package main
import (
   "fmt"
   "strings"
)

func main() {
   file := "/path/to/myfile.txt"  //assignthe variable an absolute path
   file_name := strings.TrimRight(file, "/")
   file_name = strings.Split(file_name, "/")[len(strings.Split(file_name, "/"))-1]
   fmt.Println("The File name from the absolute path is:", file_name) //print the filename
}

Output

The file Name from the absolute path is: myfile.txt

Conclusion

We executed the program of getting the name of file from the absolute path using two examples. In the first example, we used filepath.Base function and in the second example, we used TrimRight and split function to execute the program.

Updated on: 22-Feb-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements