How to find the base name of a path (the last element) using the path package in Golang?

To find the base name (last element) of a path using the path package in Golang, you can use the path.Base() function. Here's an example:

package main import ( "fmt" "path" ) func main() { filePath := "/dir/subdir/filename.txt" baseName := path.Base(filePath) fmt.Println(baseName) // Output: filename.txt }

In this example, we imported the path package and used the path.Base() function to extract the base name from the filePath variable. The fmt.Println() then outputs the base name, which is "filename.txt".