Creating & Writing to Temp Files
·
247 words
·
2 minutes read
We programmers often use temporary files and this example shows how we can create and write to one. We used the ioutil
package which has functions for just this.
The TempFile()
accepts a folder and a prefix. As the folder we get the os’ temp directory (which would be /tmp on unix systems) and for the prefix we use a string, but if we don’t want one, we just pass an empty string.
The temp files won’t be removed until you say, so we use a defer call to delete the file once we’re finished with it, with os.Remove()
.
|
|