转载地址:https://zhuanlan.zhihu.com/p/80403583
示例代码:
package main
import (
"fmt"
"path"
)
func main() {
filename := "device/sdk/CMakeLists.txt"
filenameall := path.Base(filename)
filesuffix := path.Ext(filename)
fileprefix := filenameall[0:len(filenameall) - len(filesuffix)]
//fileprefix, err := strings.TrimSuffix(filenameall, filesuffix)
fmt.Println("file name:", filenameall)
fmt.Println("file prefix:", fileprefix)
fmt.Println("file suffix:", filesuffix)
}
执行结果:
- path.Base(filename):获取不包含目录的文件名
- path.Ext(filename):获取文件后缀
文件前缀 可以使用切片的方式获取:
fileprefix := filenameall[0:len(filenameall) - len(filesuffix)]
也可以使用 strings.TrimSuffix() 进行获取:
import "strings"
fileprefix, err := strings.TrimSuffix(filenameall, filesuffix)
网友评论