DEV Community

Discussion on: Hide a file / folder using Golang

Collapse
 
hamzaanis profile image
Hamza Anis

This does not hide the file but just add the . in the prefix.

Collapse
 
tobychui profile image
Toby Chui

Add "." in the prefix is how you hide a file on Linux. On Windows, it will use syscall SetFileAttributes to set a folder to "hidden".

Collapse
 
hamzaanis profile image
Hamza Anis • Edited

In linux build

func isHidden(filename string) (bool, error) {
    if len(filepath.Base(filename)) > 0 && filepath.Base(filename)[0:1] == "." {
        return true, nil
    }

    return false, nil
}
Enter fullscreen mode Exit fullscreen mode

You are checking via

isHidden, err := hidden.IsHidden("./test", false)
Enter fullscreen mode Exit fullscreen mode

It will always end up panic because this file will be renamed to ./.test after hiding it and the above isHidden will never find the file.

For windows it should be working fine with the example in your package.

Collapse
 
hamzaanis profile image
Hamza Anis

Ok so there is another package for the windows. There is a small problem in your isHidden method and the example.

Thread Thread
 
tobychui profile image
Toby Chui

Thanks for pointing it out! I will try to fix it soon :D