Detect hidden files and directories in Linux and macOS
In Linux and Unix-like operating systems, any file or folder that starts with a
dot character (such as
.bashrc), commonly called a dotfile, is
treated as hidden by file managers and commands such as
ls. This makes it really
easy to detect a hidden file in Go source code:
All you need to do is check if the first character of the filename is a period. If it is, the file is hidden. Otherwise, it’s not. Note that 46 is the character code of a period, which is why it is used in the comparison.
Detect hidden files and directories in Windows
For Windows, the hidden status of a file or directory is not determined by
whether or not it starts with a dot character. Rather, it’s determined by a
Hidden file attribute which is manipulated through the
attrib command. You
can also view and set the status of this attribute through the file properties
Therefore, checking if a file is hidden in Windows involves checking the status
Hidden attribute as shown below:
When using the code, make sure you set build constraints on the files where these functions are placed so that they will only be included in the build for the relevant operating system.
Thanks for reading, and happy coding!