Get the App
SLTechnology News&Howtos  ›  Servers  › 

What is the subtle difference between several functions of golang's filepath package

Shulou Source: shulou.com Published: 2022-05-31 19:47:51 09月28日 Update

This article mainly introduces the subtle differences of several functions of golang's filepath package, which can be used for reference by interested friends. I hope you can learn a lot after reading this article.

Subtle differences between several functions of golang's filepath package

When Clean (), Dir (), ABS () are used with Walk (), the results will be different when traversing the directory due to the slight differences in the return values of the first three functions. Filepath.Abs (". / myDoc") / / returns the absolute path of the given path. There is no problem with traversing at this time.

10:50:31 monitoring on 2019-06-12: 1, D:\ workspace\ go-wikitten\ src\ myDoc2019/06/12 10:50:31 File: 2, myDoc\ .gitkeep2019 / 06 10:50:31 File: 3, myDoc\ Sample HTML document.html2019/06/12 10:50:31 File: 4, myDoc\ Sample Markdown document.md2019/06/12 10:50:31 Monitoring: 5 D:\ workspace\ go-wikitten\ src\ myDoc\ code snippets (expand me!) 10:50:31 on 2019-06-12 File: 6, myDoc\ code snippets (expand me!)\ Bash.sh2019/06/12 10:50:31 File: 7, myDoc\ code snippets (expand me!)\ CSS.css2019/06/12 10:50:31 File: 8, myDoc\ code snippets (expand me!)\ JavaScript.js2019/06/12 10:50:31 File: 9 MyDoc\ code snippets (expand me!)\ PHP.php2019/06/12 10:50:31 File: 10, myDoc\ code snippets (expand me!)\ Python.py2019/06/12 10:50:31 File: 11, myDoc\ code snippets (expand me!)\ Ruby.rb2019/06/12 10:50:31 File: 12, myDoc\ code snippets (expand me!)\ SQL.sql2019/06/12 10:50:31 File: 13 MyDoc\ code snippets (expand me!)\ Scheme.scm2019/06/12 10:50:31 File: 14, myDoc\ code snippets (expand me!)\ XML.xml2019/06/12 10:50:31 File: 15, myDoc\ index.md2019/06/12 10:50:31 Monitoring: 16, D:\ workspace\ go-wikitten\ src\ myDoc\ you can also2019/06/12 10:50:31 Monitoring: 17 D:\ workspace\ go-wikitten\ src\ myDoc\ you can also\ nest directories2019/06/12 10:50:31 file: 18, myDoc\ you can also\ nest directories\ binary files are OK too.jpg

Path.Clean (". / myDoc"); / / returns the equivalent shortest path / / 1. Replace multiple slashes / / 2 with one slash. Clears the current path. / / 3. Clear the internal.. And the element in front of him / / 4. With /. At the beginning, it becomes / because there is no case of 1BI 3Jet 4 in my directory, so traversal is also in line with my expectations, only replacing the absolute path with the relative path.

10:56:18 monitoring on 2019-06-12: 1, myDoc2019/06/12 10:56:18 file: 2, myDoc\ .gitkeep2019 / 06 10:56:18 file: 3, myDoc\ Sample HTML document.html2019/06/12 10:56:18 file: 4, myDoc\ Sample Markdown document.md2019/06/12 10:56:18 monitoring: 5, myDoc\ code snippets (expand me!) 10:56:18 file on 2019-06-12: 6 MyDoc\ code snippets (expand me!)\ Bash.sh2019/06/12 10:56:18 File: 7, myDoc\ code snippets (expand me!)\ CSS.css2019/06/12 10:56:18 File: 8, myDoc\ code snippets (expand me!)\ JavaScript.js2019/06/12 10:56:18 File: 9, myDoc\ code snippets (expand me!)\ PHP.php2019/06/12 10:56:18 File: 10 MyDoc\ code snippets (expand me!)\ Python.py2019/06/12 10:56:18 File: 11, myDoc\ code snippets (expand me!)\ Ruby.rb2019/06/12 10:56:18 File: 12, myDoc\ code snippets (expand me!)\ SQL.sql2019/06/12 10:56:18 File: 13, myDoc\ code snippets (expand me!)\ Scheme.scm2019/06/12 10:56:18 File: 14 MyDoc\ code snippets (expand me!)\ XML.xml2019/06/12 10:56:18 File: 15, myDoc\ index.md2019/06/12 10:56:18 Monitoring: 16, myDoc\ you can also2019/06/12 10:56:18 Monitoring: 17, myDoc\ you can also\ nest directories2019/06/12 10:56:18 File: 18, myDoc\ you can also\ nest directories\ binary files are OK too.jpg

Path.Dir (". / myDoc"); / / returns if the directory / / path of the last element of the path is empty. This traversal, at the beginning, I thought it was correct, but later I found that it was wrong. First of all, additional monitoring of the parent directory of a given directory, that is, the parent directory of myDoc, see the first line of output. Second, if there are no files in the directory, then the directory, see lines 17 and 18 of the output, the traversal is skipped.

10:48:30 Monitoring on 2019-06-12: 1, 2019 / 06 10:48:30 File: 2, myDoc\ .gitkeep2019 / 06 Sample HTML document.html2019/06/12 12 10:48:30 File: 3, myDoc\ Sample HTML document.html2019/06/12 10:48:30 File: 4, myDoc\ Sample Markdown document.md2019/06/12 10:48:30 Monitoring: 5, myDoc2019/06/12 10:48:30 File: 6 MyDoc\ code snippets (expand me!)\ Bash.sh2019/06/12 10:48:30 File: 7, myDoc\ code snippets (expand me!)\ CSS.css2019/06/12 10:48:30 File: 8, myDoc\ code snippets (expand me!)\ JavaScript.js2019/06/12 10:48:30 File: 9, myDoc\ code snippets (expand me!)\ PHP.php2019/06/12 10:48:30 File: 10 MyDoc\ code snippets (expand me!)\ Python.py2019/06/12 10:48:30 File: 11, myDoc\ code snippets (expand me!)\ Ruby.rb2019/06/12 10:48:30 File: 12, myDoc\ code snippets (expand me!)\ SQL.sql2019/06/12 10:48:30 File: 13, myDoc\ code snippets (expand me!)\ Scheme.scm2019/06/12 10:48:30 File: 14 MyDoc\ code snippets (expand me!)\ XML.xml2019/06/12 10:48:30 File: 15, myDoc\ index.md2019/06/12 10:48:30 Monitoring: 16, myDoc2019/06/12 10:48:30 Monitoring: 17, myDoc\ you can also2019/06/12 10:48:30 File: 18, myDoc\ you can also\ nest directories\ binary files are OK too.jpg Thank you for reading this article carefully I hope the article "what are the subtle differences between several functions of golang's filepath package" shared by the editor will be helpful to you. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

Tags: File monitor directory path nuance function time article element slash output no one line three that is value interest just at the same time multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MySQL Docker Redmi Shulou Technology