package main
import (
"fmt"
"path/filepath"
"strings"
)
/*
RemoveFileExtension removes the last file extension from a filename or path.
Examples:
document.pdf -> document
archive.tar.gz -> archive.tar
photo.jpg -> photo
README -> README
/tmp/file.txt -> /tmp/file
Parameters:
path - The filename or path.
Returns:
The filename/path without its last extension.
*/
func RemoveFileExtension(path string) string {
// Get the last extension, including the leading dot.
ext := filepath.Ext(path)
// No extension found.
if ext == "" {
return path
}
// Remove the extension from the end of the path.
return strings.TrimSuffix(path, ext)
}
func main() {
fmt.Println(RemoveFileExtension("document.pdf"))
fmt.Println(RemoveFileExtension("archive.tar.gz"))
fmt.Println(RemoveFileExtension("/tmp/file.txt"))
fmt.Println(RemoveFileExtension("README"))
}
/*
run:
document
archive.tar
/tmp/file
README
*/