From 447d77234f9d90ed8cf9b938f38dddc5fa021641 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sat, 8 Jun 2019 06:11:13 -0700 Subject: [PATCH] Add directories example --- examples.txt | 1 + examples/directories/directories.go | 77 +++++++ examples/directories/directories.hash | 2 + examples/directories/directories.sh | 7 + public/directories | 297 ++++++++++++++++++++++++++ public/file-paths | 2 +- public/index.html | 2 + 7 files changed, 387 insertions(+), 1 deletion(-) create mode 100644 examples/directories/directories.go create mode 100644 examples/directories/directories.hash create mode 100644 examples/directories/directories.sh create mode 100644 public/directories diff --git a/examples.txt b/examples.txt index 91a1fde..887ef69 100644 --- a/examples.txt +++ b/examples.txt @@ -58,6 +58,7 @@ Reading Files Writing Files Line Filters File Paths +Directories Command-Line Arguments Command-Line Flags Command-Line Subcommands diff --git a/examples/directories/directories.go b/examples/directories/directories.go new file mode 100644 index 0000000..1d4eec9 --- /dev/null +++ b/examples/directories/directories.go @@ -0,0 +1,77 @@ +// Go has several useful functions for working with +// *directories* in the file system. + +package main + +import ( + "fmt" + "io/ioutil" + "os" +) + +func check(e error) { + if e != nil { + panic(e) + } +} + +func main() { + + // Create a new sub-directory in the current working + // directory. + err := os.Mkdir("subdir", 0755) + check(err) + + // When creating temporary directories, it's good + // practice to `defer` their removal. `os.RemoveAll` + // will delete a whole directory tree (similarly to + // `rm -rf`). + defer os.RemoveAll("subdir") + + // Helper function to create a new empty file. + createEmptyFile := func(name string) { + d := []byte("") + check(ioutil.WriteFile(name, d, 0644)) + } + + createEmptyFile("subdir/file1") + + // We can create a hierarchy of directories, including + // parents wiht `MkdirAll`. This is similar to the + // command-line `mkdir -p`. + err = os.MkdirAll("subdir/parent/child", 0755) + check(err) + + createEmptyFile("subdir/parent/file2") + createEmptyFile("subdir/parent/file3") + createEmptyFile("subdir/parent/child/file4") + + // `ReadDir` lists directory contents, returning a + // slice of `os.FileInfo` objects. + c, err := ioutil.ReadDir("subdir/parent") + check(err) + + fmt.Println("Listing subdir/parent") + for _, entry := range c { + fmt.Println(entry.Name(), entry.IsDir()) + } + + // `Chdir` lets us change the current working directory, + // similarly to `cd`. + err = os.Chdir("subdir/parent/child") + check(err) + + // Now we'll see the contents of "subdir/parent/child" + // when listing the *current* directory. + c, err = ioutil.ReadDir(".") + check(err) + + fmt.Println("Listing subdir/parent/child") + for _, entry := range c { + fmt.Println(entry.Name(), entry.IsDir()) + } + + // `cd` back to where we started. + err = os.Chdir("../../..") + check(err) +} diff --git a/examples/directories/directories.hash b/examples/directories/directories.hash new file mode 100644 index 0000000..b599edd --- /dev/null +++ b/examples/directories/directories.hash @@ -0,0 +1,2 @@ +7b2eed223a00c8a84df582bd254a642c7a57dd9b +UnjBL6NmR8- diff --git a/examples/directories/directories.sh b/examples/directories/directories.sh new file mode 100644 index 0000000..cc48f74 --- /dev/null +++ b/examples/directories/directories.sh @@ -0,0 +1,7 @@ +$ go run directories.go +Listing subdir/parent +child true +file2 false +file3 false +Listing subdir/parent/child +file4 false diff --git a/public/directories b/public/directories new file mode 100644 index 0000000..f80b48f --- /dev/null +++ b/public/directories @@ -0,0 +1,297 @@ + + + + + Go by Example: Directories + + + +
+

Go by Example: Directories

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

Go has several useful functions for working with +directories in the file system.

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

Create a new sub-directory in the current working +directory.

+ +
+ +
    err := os.Mkdir("subdir", 0755)
+    check(err)
+
+ +
+

When creating temporary directories, it’s good +practice to defer their removal. os.RemoveAll +will delete a whole directory tree (similarly to +rm -rf).

+ +
+ +
    defer os.RemoveAll("subdir")
+
+ +
+

Helper function to create a new empty file.

+ +
+ +
    createEmptyFile := func(name string) {
+        d := []byte("")
+        check(ioutil.WriteFile(name, d, 0644))
+    }
+
+ +
+ + + +
    createEmptyFile("subdir/file1")
+
+ +
+

We can create a hierarchy of directories, including +parents wiht MkdirAll. This is similar to the +command-line mkdir -p.

+ +
+ +
    err = os.MkdirAll("subdir/parent/child", 0755)
+    check(err)
+
+ +
+ + + +
    createEmptyFile("subdir/parent/file2")
+    createEmptyFile("subdir/parent/file3")
+    createEmptyFile("subdir/parent/child/file4")
+
+ +
+

ReadDir lists directory contents, returning a +slice of os.FileInfo objects.

+ +
+ +
    c, err := ioutil.ReadDir("subdir/parent")
+    check(err)
+
+ +
+ + + +
    fmt.Println("Listing subdir/parent")
+    for _, entry := range c {
+        fmt.Println(entry.Name(), entry.IsDir())
+    }
+
+ +
+

Chdir lets us change the current working directory, +similarly to cd.

+ +
+ +
    err = os.Chdir("subdir/parent/child")
+    check(err)
+
+ +
+

Now we’ll see the contents of “subdir/parent/child” +when listing the current directory.

+ +
+ +
    c, err = ioutil.ReadDir(".")
+    check(err)
+
+ +
+ + + +
    fmt.Println("Listing subdir/parent/child")
+    for _, entry := range c {
+        fmt.Println(entry.Name(), entry.IsDir())
+    }
+
+ +
+

cd back to where we started.

+ +
+ +
    err = os.Chdir("../../..")
+    check(err)
+}
+
+ +
+ + + + + + + + +
+ + + +
$ go run directories.go
+Listing subdir/parent
+child true
+file2 false
+file3 false
+Listing subdir/parent/child
+file4 false
+
+ +
+ + +

+ Next example: Command-Line Arguments. +

+ + +
+ + diff --git a/public/file-paths b/public/file-paths index 65e3886..f7b24a1 100644 --- a/public/file-paths +++ b/public/file-paths @@ -229,7 +229,7 @@ be made relative to base.

- Next example: Command-Line Arguments. + Next example: Directories.

  • File Paths
  • +
  • Directories
  • +
  • Command-Line Arguments
  • Command-Line Flags