Cross-Platform CLI Notes
Cross-Platform CLI Notes
Overview
Go builds everywhere; OS details still leak into CLIs. Isolate differences.
Paths
filepath.Join(a, b) // not strings + "/"
filepath.Separator
os.UserConfigDir() // not hardcode ~/.configLine endings
// accept both when reading tools
// write "\n" unless you must speak CRLF to Windows toolsSignals
Windows: os.Interrupt works for Ctrl-C; SIGTERM semantics differ. Use signal.NotifyContext with available signals; build-tag extra Unix signals.
Shells
- Avoid
cmd /cwith user strings
- PowerShell quoting ≠ bash
- Document examples for both
Color
// enable only if TTY && NO_COLOR unset && TERM != dumbWindows: optional virtual terminal processing for ANSI.
File locking
flock Unix-only; Windows has different APIs—feature-detect or document Unix-only commands.
Rules
| Do | Don’t |
|---|---|
filepath + os helpers |
Assume /tmp exists as on Linux |
| CI matrix linux/mac/windows | Test only on your laptop OS |
| Clear “unix only” in help | Silent fail on Windows |
Try next
GOOS=windows go buildyour CLI.
- Replace hardcoded
/tmpwithos.TempDir().
- Stub flock commands on Windows with message.