func main() {
//拼接路径
p := filepath.Join("dir1", "dir2", "filename")
fmt.Println("p:", p) //p: dir1\dir2\filename
fmt.Println(filepath.Join("dir1//", "filename")) //dir1\filename
fmt.Println(filepath.Join("dir1/../dir1", "filename")) //dir1\filename
fmt.Println("Dir(p):", filepath.Dir(p)) //Dir(p): dir1\dir2
fmt.Println("Base(p):", filepath.Base(p)) //Base(p): filename
//IsAbs返回路径是否是一个绝对路径
fmt.Println(filepath.IsAbs("dir/file")) //false
fmt.Println(filepath.IsAbs("/dir/file")) //false
filename := "config.json"
//Ext函数返回path文件扩展名
ext := filepath.Ext(filename)
fmt.Println("ext:",ext) //ext: .json
//返回去除filename可能的后缀ext的字符串
fmt.Println(strings.TrimSuffix(filename, ext)) //config
rel, err := filepath.Rel("a/b", "a/b/t/file")
if err != nil {
panic(err)
}
fmt.Println("rel1:",rel) //rel1: t\file
rel, err = filepath.Rel("a/b", "a/c/t/file")
if err != nil {
panic(err)
}
fmt.Println("rel2:",rel) //rel2: ..\c\t\file
}