Append_file!
append_file! adds text to the end of a file without replacing its existing contents.
Overview
Use append_file! when a file should keep its current contents and receive additional text.
append_file!("logs/adventure.log", "Entered the crystal caves\n")
append_file!("logs/adventure.log", "Entered the crystal caves\n")
If adventure.log already contains text, the new line is written after it.
If the file does not exist, Goblin creates it.
It Does Not Add a Newline
append_file! writes the text exactly as provided. Add \n yourself when appending lines:
append_file!("logs/adventure.log", "Found the hidden gate\n") append_file!("logs/adventure.log", "Opened the gate\n")
append_file!("logs/adventure.log", "Found the hidden gate\n") append_file!("logs/adventure.log", "Opened the gate\n")
Without the newline characters, the text is joined together:
append_file!("notes.txt", "north") append_file!("notes.txt", "tunnel") /// notes.txt now contains: northtunnel
append_file!("notes.txt", "north") append_file!("notes.txt", "tunnel") /// notes.txt now contains: northtunnel
Mutation Form Required
Appending changes the filesystem, so append_file requires the ! form:
append_file!("notes.txt", "Bring more rope\n")
append_file!("notes.txt", "Bring more rope\n")
Calling it without ! raises an error:
append_file("notes.txt", "Bring more rope\n") /// error: M0001 mutation-operator-required
append_file("notes.txt", "Bring more rope\n") /// error: M0001 mutation-operator-required
Signature
append_file!(path, text)
append_file!(path, text)
| Argument | Type | Description |
|---|---|---|
path |
string | The file to create or append to. |
text |
string | The text written at the end of the file. |
append_file! returns unit.
The file is created when it is missing, but its parent directory must already exist.
Errors
append_file! requires exactly two arguments:
append_file!("notes.txt") /// error: R0301 wrong-arity
append_file!("notes.txt") /// error: R0301 wrong-arity
Both arguments must be strings:
append_file!("notes.txt", 42) /// error: T0205 type-mismatch
append_file!("notes.txt", 42) /// error: T0205 type-mismatch
Goblin raises a filesystem error if it cannot open or write to the file:
FS0001 filesystem-io
FS0001 filesystem-io
This can happen when the parent directory is missing, permissions deny access, or the disk cannot accept more data.