From 8219122e44d0dd7a40b082db37a0baed94246204 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Mon, 17 Jun 2019 06:01:41 -0700 Subject: [PATCH] Temporary files and directories sample Followup on discussion in #243 --- examples.txt | 1 + .../temporary-files-and-directories.go | 65 +++++ .../temporary-files-and-directories.hash | 2 + .../temporary-files-and-directories.sh | 4 + public/index.html | 2 + public/temporary-files-and-directories | 232 ++++++++++++++++++ public/writing-files | 2 +- 7 files changed, 307 insertions(+), 1 deletion(-) create mode 100644 examples/temporary-files-and-directories/temporary-files-and-directories.go create mode 100644 examples/temporary-files-and-directories/temporary-files-and-directories.hash create mode 100644 examples/temporary-files-and-directories/temporary-files-and-directories.sh create mode 100644 public/temporary-files-and-directories diff --git a/examples.txt b/examples.txt index 887ef69..ae601df 100644 --- a/examples.txt +++ b/examples.txt @@ -56,6 +56,7 @@ SHA1 Hashes Base64 Encoding Reading Files Writing Files +Temporary Files and Directories Line Filters File Paths Directories diff --git a/examples/temporary-files-and-directories/temporary-files-and-directories.go b/examples/temporary-files-and-directories/temporary-files-and-directories.go new file mode 100644 index 0000000..abed14e --- /dev/null +++ b/examples/temporary-files-and-directories/temporary-files-and-directories.go @@ -0,0 +1,65 @@ +// Throughout program execution, we often want to create +// data that isn't needed after the program exits. +// *Temporary files and directories* are useful for this +// purpose since they don't pollute the file system over +// time. + +package main + +import ( + "fmt" + "io/ioutil" + "os" + "path/filepath" +) + +func check(e error) { + if e != nil { + panic(e) + } +} + +func main() { + + // The easiest way to create a temporary file is by + // calling `ioutil.TempFile`. It creates a file *and* + // opens it for reading and writing. We provide `""` + // as the first argument, so `ioutil.TempFile` will + // create the file in the default location for our OS. + f, err := ioutil.TempFile("", "sample") + check(err) + + // Display the name of the temporary file. On + // Unix-based OSes the directory will likely be `/tmp`. + // The file name starts with the prefix given as the + // second argument to `ioutil.TempFile` and the rest + // is chosen automatically to ensure that concurrent + // calls will always create different file names. + fmt.Println("Temp file name:", f.Name()) + + // Clean up the file after we're done. The OS is + // likely to clean up temporary files by itself after + // some time, but it's good practice to do this + // explicitly. + defer os.Remove(f.Name()) + + // We can write some data to the file. + _, err = f.Write([]byte{1, 2, 3, 4}) + check(err) + + // If we intend to write many temporary files, we may + // prefer to create a temporary *directory*. + // `ioutil.TempDir`'s arguments are the same as + // `TempFile`'s, but it returns a directory *name* + // rather than an open file. + dname, err := ioutil.TempDir("", "sampledir") + fmt.Println("Temp dir name:", dname) + + defer os.RemoveAll(dname) + + // Now we can synthesize temporary file names by + // prefixing them with our temporary directory. + fname := filepath.Join(dname, "file1") + err = ioutil.WriteFile(fname, []byte{1, 2}, 0666) + check(err) +} diff --git a/examples/temporary-files-and-directories/temporary-files-and-directories.hash b/examples/temporary-files-and-directories/temporary-files-and-directories.hash new file mode 100644 index 0000000..f523002 --- /dev/null +++ b/examples/temporary-files-and-directories/temporary-files-and-directories.hash @@ -0,0 +1,2 @@ +5f7d0c43988d7dce235adb06ec02f4d2026b7f83 +pxE20wGTFjv diff --git a/examples/temporary-files-and-directories/temporary-files-and-directories.sh b/examples/temporary-files-and-directories/temporary-files-and-directories.sh new file mode 100644 index 0000000..1854a19 --- /dev/null +++ b/examples/temporary-files-and-directories/temporary-files-and-directories.sh @@ -0,0 +1,4 @@ +# The exact names printed out will likely be different. +$ go run temporary-files-and-directories.go +Temp file name: /tmp/sample610887201 +Temp dir name: /tmp/sampledir898854668 diff --git a/public/index.html b/public/index.html index b11b9ef..e3cb506 100644 --- a/public/index.html +++ b/public/index.html @@ -139,6 +139,8 @@
  • Writing Files
  • +
  • Temporary Files and Directories
  • +
  • Line Filters
  • File Paths
  • diff --git a/public/temporary-files-and-directories b/public/temporary-files-and-directories new file mode 100644 index 0000000..e8336ca --- /dev/null +++ b/public/temporary-files-and-directories @@ -0,0 +1,232 @@ + + + + + Go by Example: Temporary Files and Directories + + + +
    +

    Go by Example: Temporary Files and Directories

    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +

    Throughout program execution, we often want to create +data that isn’t needed after the program exits. +Temporary files and directories are useful for this +purpose since they don’t pollute the file system over +time.

    + +
    + + +
    + + + + + +
    package main
    +
    + +
    + + + +
    import (
    +    "fmt"
    +    "io/ioutil"
    +    "os"
    +    "path/filepath"
    +)
    +
    + +
    + + + +
    func check(e error) {
    +    if e != nil {
    +        panic(e)
    +    }
    +}
    +
    + +
    + + + +
    func main() {
    +
    + +
    +

    The easiest way to create a temporary file is by +calling ioutil.TempFile. It creates a file and +opens it for reading and writing. We provide "" +as the first argument, so ioutil.TempFile will +create the file in the default location for our OS.

    + +
    + +
        f, err := ioutil.TempFile("", "sample")
    +    check(err)
    +
    + +
    +

    Display the name of the temporary file. On +Unix-based OSes the directory will likely be /tmp. +The file name starts with the prefix given as the +second argument to ioutil.TempFile and the rest +is chosen automatically to ensure that concurrent +calls will always create different file names.

    + +
    + +
        fmt.Println("Temp file name:", f.Name())
    +
    + +
    +

    Clean up the file after we’re done. The OS is +likely to clean up temporary files by itself after +some time, but it’s good practice to do this +explicitly.

    + +
    + +
        defer os.Remove(f.Name())
    +
    + +
    +

    We can write some data to the file.

    + +
    + +
        _, err = f.Write([]byte{1, 2, 3, 4})
    +    check(err)
    +
    + +
    +

    If we intend to write many temporary files, we may +prefer to create a temporary directory. +ioutil.TempDir’s arguments are the same as +TempFile’s, but it returns a directory name +rather than an open file.

    + +
    + +
        dname, err := ioutil.TempDir("", "sampledir")
    +    fmt.Println("Temp dir name:", dname)
    +
    + +
    + + + +
        defer os.RemoveAll(dname)
    +
    + +
    +

    Now we can synthesize temporary file names by +prefixing them with our temporary directory.

    + +
    + +
        fname := filepath.Join(dname, "file1")
    +    err = ioutil.WriteFile(fname, []byte{1, 2}, 0666)
    +    check(err)
    +}
    +
    + +
    + + + + + + + + +
    +

    The exact names printed out will likely be different.

    + +
    + +
    $ go run temporary-files-and-directories.go
    +Temp file name: /tmp/sample610887201
    +Temp dir name: /tmp/sampledir898854668
    +
    + +
    + + +

    + Next example: Line Filters. +

    + + +
    + + diff --git a/public/writing-files b/public/writing-files index ca974bd..98401c2 100644 --- a/public/writing-files +++ b/public/writing-files @@ -266,7 +266,7 @@ we’ve just seen to the stdin and stdout streams.

    - Next example: Line Filters. + Next example: Temporary Files and Directories.