filesystem
dir, err := os.MkdirTemp("", "tool-")
if err != nil {
return err
}
defer os.RemoveAll(dir)
file := filepath.Join(dir, "note.txt")
if err := os.WriteFile(file, []byte("payload\n"), 0600); err != nil {
return err
}
data, err := os.ReadFile(file)
Output value:
payload
Temp first
Create a directory owned by this run
Write there
Delete it with defer
That pattern prevents leftovers and collisions
Permissions
0600 means owner read and write only
Use 0755 for directories that must be traversed
Secrets never get world readable modes
Paths
Join with filepath.Join
Do not concatenate strings with /
Windows paths are the reason
Errors
Every filesystem call can fail Check the error at the call A deferred cleanup that ignores its error is acceptable, a silent write failure is not
Next
HTTP in 08 http
Posted