Go实例: File Paths

Bliss ·
更新时间:2024-11-13
· 389 次阅读

package main import ( "fmt" "path/filepath" "strings" ) func main() { p := filepath.Join("dir1", "dir2", "filename") fmt.Println("p:", p) fmt.Println(filepath.Join("dir1//", "filename")) fmt.Println(filepath.Join("dir1/../dir1", "filename")) fmt.Println("Dir(p):", filepath.Dir(p)) fmt.Println("Base(p):", filepath.Base(p)) fmt.Println(filepath.IsAbs("dir/file")) fmt.Println(filepath.IsAbs("/dir/file")) filename := "config.json" ext := filepath.Ext(filename) fmt.Println(ext) fmt.Println(strings.TrimSuffix(filename, ext)) rel, err := filepath.Rel("a/b", "a/b/t/file") if err != nil { panic(err) } fmt.Println(rel) rel, err = filepath.Rel("a/b", "a/c/t/file") if err != nil { panic(err) } fmt.Println(rel) }$ go run file-paths.go p: dir1/dir2/filename dir1/filename dir1/filename Dir(p): dir1/dir2 Base(p): filename false true .json config t/file ../c/t/file



file GO GO实例

需要 登录 后方可回复, 如果你还没有账号请 注册新账号