2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
32
Task/Secure-temporary-file/Go/secure-temporary-file.go
Normal file
32
Task/Secure-temporary-file/Go/secure-temporary-file.go
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
f, err := ioutil.TempFile("", "foo")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// We need to make sure we remove the file
|
||||
// once it is no longer needed.
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
// … use the file via 'f' …
|
||||
fmt.Fprintln(f, "Using temporary file:", f.Name())
|
||||
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).
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue