Go code
err := os.WriteFile("note.txt", []byte("Hello"), 0644)
if err != nil {
fmt.Println("Could not write file")
return
}
fmt.Println("Saved")Line-by-line explanation
- WriteFile selects the path.
- The string is converted to bytes.
- 0644 is the new file permission on Unix-like systems.
- The error is checked.
- Saved confirms success.
Expected output
SavedThis is the expected result.
WriteFile replaces an existing file. The permission value matters when a new file is created.