Chapter 4 Navigating the File System
Powerful Command-Line Applications in Go — by Ricardo Gerardi (39 / 127)
👈 Wrapping Up | TOC | Developing a File System Crawler 👉
When you’re developing your own command-line tools, you’ll often need to interact with the file system, navigate around the directory tree, or perform actions on files or directories. When you’re building a tool that’s supposed to work on multiple platforms, you have to pay special attention to how you manipulate paths and file names to ensure your tool works appropriately no matter where it’s run.
For example, Windows uses a backslash \ as a path separator, such as C:\WINDOWS\SYSTEM, while most variations of UNIX use the forward slash / instead, as in /usr/lib. In these scenarios, hard-coding paths and file names into the program can lead to errors or unexpected results.
To avoid these complications, Go provides the filepath package to manipulate paths, ensuring compatibility across different operating systems. You’ll use this package to develop a command-line tool called walk, which crawls into file system directories looking for specific files. When the tool finds the files it’s looking for, it can list, archive, or delete them. By developing this tool, you’ll apply the skills required to handle file system objects in your own programs, such as creating directories, copying and deleting files, and handling logs and compressed files. You’ll also end up with a useful tool that helps you back up…