Go語言中Path包用法

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
// 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}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.