go获取文件名和后缀

作者: adm 分类: go 发布时间: 2021-11-17

go获取文件名和后缀

fullFilename := "D:/software/Typora/bin/typora.exe"
fmt.Println("fullFilename =", fullFilename)
//获取文件名带后缀
filenameWithSuffix := path.Base(fullFilename)
fmt.Println("filenameWithSuffix =", filenameWithSuffix)
//获取文件后缀
fileSuffix := path.Ext(filenameWithSuffix)
fmt.Println("fileSuffix =", fileSuffix)

//获取文件名
filenameOnly := strings.TrimSuffix(filenameWithSuffix, fileSuffix)
fmt.Println("filenameOnly =", filenameOnly)
————————————————

输出结果如下

fullFilename = D:/software/Typora/bin/typora.exe
filenameWithSuffix = typora.exe
fileSuffix = .exe
filenameOnly = typora

如果觉得我的文章对您有用,请随意赞赏。您的支持将鼓励我继续创作!