Anonymous user
Secure temporary file: Difference between revisions
→{{header|Go}}: Explicitly close the temporary file; also actually use the file a little bit (grr.. damn recaptcha crap)
(→{{header|Go}}: Expand example to a run-able program that shows the file name and removes it) |
(→{{header|Go}}: Explicitly close the temporary file; also actually use the file a little bit (grr.. damn recaptcha crap)) |
||
Line 127:
=={{header|Go}}==
Use <code>[https://golang.org/pkg/io/ioutil/#TempFile ioutil.TempFile]</code>
<lang go>package main
Line 141 ⟶ 142:
log.Fatal(err)
}
defer f.Close()
// Make sure we remove the file once we're done.▼
// once it is no longer needed.
defer os.Remove(f.Name())
// … use the file via 'f' …
fmt.
f.Seek(0, 0)
d, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Wrote and read: %s\n", d)
// The defer statements above will close and remove the
// temporary file here (or on any return of this function).
}</lang>
{{out}}
<pre>
Wrote and read: Using temporary file: /tmp/foo054003078
</pre>
|