這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
// pathpackage mainimport ("fmt""os""path""path/filepath")func main() {//Path操作fmt.Println("Path操作-----------------")fmt.Println(path.Base("http://www.baidu.com/file/aa.jpg")) //aa.jpgfmt.Println(path.Clean("c:\\file//abc///aa.jpg")) //c:\file/abc/aa.jpgfmt.Println(os.Getwd()) //D:\Projects\GoPath\source\demo\syntax\path <nil>fmt.Println(path.Dir("http://www.baidu.com/aa/aaa.jpg")) //http:/www.baidu.com/aafmt.Println(path.Dir("c:/a/b/c/d.txt")) //c:/a/b/cfmt.Println(path.Dir("c:\\a/b.txt")) //c:\afmt.Println(path.Ext("c:\\a/b.txt")) //.txtfmt.Println(path.IsAbs("c:/wind/aa/bb/b.txt")) //falsefmt.Println(path.Join("c:", "aa", "bb", "cc.txt")) //c:/aa/bb/cc.txtisMatch, err := path.Match("c:/windows/*/", "c:/windows/system/")fmt.Println(isMatch, err) //true <nil>fmt.Println(path.Split("c:/windows/system/aaa.jpg")) //c:/windows/system/ aaa.jpg//FilePath操作fmt.Println("FilePath操作-----------------")fmt.Println(filepath.IsAbs("c:\\wind\\aa\\bb\\b.txt")) //truefmt.Println(filepath.Abs(".")) //D:\Projects\GoPath\source\demo\syntax\path <nil>fmt.Println(filepath.Base("c:\\aa\\baa.exe")) //baa.exefmt.Println(filepath.Clean("c:\\\\aa/c\\baa.exe")) //c:\aa\c\baa.exefmt.Println(filepath.Clean("aa/c\\baa.exe")) //aa\c\baa.exefmt.Println(filepath.Dir("aa/c\\baa.exe")) //aa\cfmt.Println(filepath.EvalSymlinks("./path.exe")) //可以用來判斷檔案或檔案夾是否存在。 //path.exe <nil>fmt.Println(filepath.Ext("./path.exe")) //.exefmt.Println(filepath.FromSlash("c:\\windows\\aa//bb/cc//path.exe")) //將路徑中的\\更換為/ //c:\windows\aa\\bb\cc\\path.exefmt.Println(filepath.ToSlash("c:\\windows\\aa/bb/cc/path.exe")) //將路徑中的/替換為\\ //c:/windows/aa/bb/cc/path.exefmt.Println(filepath.VolumeName("c:\\windows\\")) //擷取卷標 //c:fmt.Println(filepath.Glob("c:\\windows\\*.exe")) //擷取所有c:\\windows\\目錄下exe檔案。fmt.Println(filepath.HasPrefix("c:\\aa\\bb", "c:\\")) //truefmt.Println(filepath.IsAbs("http://www.baidu.com/aa.jpg")) //falsefmt.Println(filepath.Join("a", "\\bb\\", "cc", "/d", "e\\", "ff.txt")) //a\bb\cc\d\e\ff.txtfmt.Println(filepath.Match("c:/windows/*/", "c:/windows/system/")) //true <nil>fmt.Println(filepath.Rel("c:/windows", "c:/windows/system/")) //取得第二參的路徑中,相對於前面的路徑的相對路徑。 //system <nil>fmt.Println(string(filepath.Separator)) // windows下返回\\fmt.Println(filepath.Split("c:/windows/system/abc.exe")) //c:/windows/system/ abc.exefmt.Println(filepath.SplitList("c:/windows/system/abc.exe")) //[c:/windows/system/abc.exe]filepath.Walk("../../syntax", WalkFunc)/* File: ../../syntax IsDir: true size: 0 File: ..\..\syntax\painc IsDir: true size: 0 File: ..\..\syntax\painc\main.go IsDir: false size: 813 File: ..\..\syntax\painc\painc.exe IsDir: false size: 2498048 File: ..\..\syntax\path IsDir: true size: 0 File: ..\..\syntax\path\path.exe IsDir: false size: 2851328 File: ..\..\syntax\path\path.go IsDir: false size: 3419*/}func WalkFunc(path string, info os.FileInfo, err error) error {fmt.Println("File:", path, "IsDir:", info.IsDir(), "size:", info.Size())return nil}