To extract the filename from a file 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 := "/path/to/file.txt"
fileName := path.Base(filePath)
fmt.Println(fileName) // Output: file.txt
}
In this example, we import the path
package, which provides various functions for working with file paths. We define a file path (filePath
) and then use the path.Base()
function to extract the filename from the path. Finally, we print the extracted filename (file.txt
) to the console.