Go Directories
Working with directories is a core part of many Go applications. You'll primarily use the os package for these tasks.
1. Basic Operations
2. Listing Directory Contents
Use os.ReadDir to get a list of files and folders inside a directory.
3. Change Working Directory
You can change where your program searches for files using Chdir.
Linux Permission Codes (The 0755)
When creating directories or files, Go uses numeric permission codes (octal).
| Code | Meaning |
|---|---|
0755 |
Everyone can read/execute; only owner can write. (Standard for folders) |
0644 |
Everyone can read; only owner can write. (Standard for files) |
0700 |
Only the owner can do anything. (Private) |
0777 |
Everyone can do everything. (Not recommended) |
Why use MkdirAll?
It is "idempotent." If the directory already exists, it doesn't return an error; it just does nothing. This makes it safer for setup scripts.