From 447d77234f9d90ed8cf9b938f38dddc5fa021641 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sat, 8 Jun 2019 06:11:13 -0700 Subject: [PATCH 1/4] 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
  • From ee8c401ac383887e769f0348a4df6f5a11e38836 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sat, 8 Jun 2019 11:11:58 -0700 Subject: [PATCH 2/4] Update examples/directories/directories.go Co-Authored-By: Mark McGranaghan --- examples/directories/directories.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/directories/directories.go b/examples/directories/directories.go index 1d4eec9..0384354 100644 --- a/examples/directories/directories.go +++ b/examples/directories/directories.go @@ -37,7 +37,7 @@ func main() { createEmptyFile("subdir/file1") // We can create a hierarchy of directories, including - // parents wiht `MkdirAll`. This is similar to the + // parents with `MkdirAll`. This is similar to the // command-line `mkdir -p`. err = os.MkdirAll("subdir/parent/child", 0755) check(err) From 1be2c1c9100d53294505f42e9d8e468a4cbe7fce Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Sat, 8 Jun 2019 11:20:00 -0700 Subject: [PATCH 3/4] Add Walk --- examples/directories/directories.go | 22 ++++++++- examples/directories/directories.hash | 4 +- examples/directories/directories.sh | 16 +++++-- public/directories | 64 +++++++++++++++++++++++---- 4 files changed, 89 insertions(+), 17 deletions(-) diff --git a/examples/directories/directories.go b/examples/directories/directories.go index 0384354..8abdb51 100644 --- a/examples/directories/directories.go +++ b/examples/directories/directories.go @@ -7,6 +7,7 @@ import ( "fmt" "io/ioutil" "os" + "path/filepath" ) func check(e error) { @@ -53,7 +54,7 @@ func main() { fmt.Println("Listing subdir/parent") for _, entry := range c { - fmt.Println(entry.Name(), entry.IsDir()) + fmt.Println(" ", entry.Name(), entry.IsDir()) } // `Chdir` lets us change the current working directory, @@ -68,10 +69,27 @@ func main() { fmt.Println("Listing subdir/parent/child") for _, entry := range c { - fmt.Println(entry.Name(), entry.IsDir()) + fmt.Println(" ", entry.Name(), entry.IsDir()) } // `cd` back to where we started. err = os.Chdir("../../..") check(err) + + // We can also visit a directory *recursively*, + // including all its sub-directories. `Walk` accepts + // a callback function to handle every file or + // directory visited. + fmt.Println("Visiting subdir") + err = filepath.Walk("subdir", visit) +} + +// visit is called for every file or directory found +// recursively by `filepath.Walk`. +func visit(p string, info os.FileInfo, err error) error { + if err != nil { + return err + } + fmt.Println(" ", p, info.IsDir()) + return nil } diff --git a/examples/directories/directories.hash b/examples/directories/directories.hash index b599edd..75cb9fa 100644 --- a/examples/directories/directories.hash +++ b/examples/directories/directories.hash @@ -1,2 +1,2 @@ -7b2eed223a00c8a84df582bd254a642c7a57dd9b -UnjBL6NmR8- +57a8f629f040270c15a0375be4424b392edf3c95 +LI7ty_KDozd diff --git a/examples/directories/directories.sh b/examples/directories/directories.sh index cc48f74..31a8f0c 100644 --- a/examples/directories/directories.sh +++ b/examples/directories/directories.sh @@ -1,7 +1,15 @@ $ go run directories.go Listing subdir/parent -child true -file2 false -file3 false + child true + file2 false + file3 false Listing subdir/parent/child -file4 false + file4 false +Visiting subdir + subdir true + subdir/file1 false + subdir/parent true + subdir/parent/child true + subdir/parent/child/file4 false + subdir/parent/file2 false + subdir/parent/file3 false diff --git a/public/directories b/public/directories index f80b48f..a1bfce5 100644 --- a/public/directories +++ b/public/directories @@ -28,7 +28,7 @@ - +
    package main
    @@ -47,6 +47,7 @@
         "fmt"
         "io/ioutil"
         "os"
    +    "path/filepath"
     )
     
    @@ -143,7 +144,7 @@ will delete a whole directory tree (similarly to

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

    @@ -193,7 +194,7 @@ slice of os.FileInfo objects.

        fmt.Println("Listing subdir/parent")
         for _, entry := range c {
    -        fmt.Println(entry.Name(), entry.IsDir())
    +        fmt.Println(" ", entry.Name(), entry.IsDir())
         }
     
    @@ -238,7 +239,7 @@ when listing the current directory.

        fmt.Println("Listing subdir/parent/child")
         for _, entry := range c {
    -        fmt.Println(entry.Name(), entry.IsDir())
    +        fmt.Println(" ", entry.Name(), entry.IsDir())
         }
     
    @@ -250,10 +251,47 @@ when listing the current directory.

    cd back to where we started.

    - +
        err = os.Chdir("../../..")
         check(err)
    +
    + + + + + + +

    We can also visit a directory recursively, +including all its sub-directories. Walk accepts +a callback function to handle every file or +directory visited.

    + + + + +
        fmt.Println("Visiting subdir")
    +    err = filepath.Walk("subdir", visit)
    +}
    +
    + + + + + + +

    visit is called for every file or directory found +recursively by filepath.Walk.

    + + + + +
    func visit(p string, info os.FileInfo, err error) error {
    +    if err != nil {
    +        return err
    +    }
    +    fmt.Println(" ", p, info.IsDir())
    +    return nil
     }
     
    @@ -272,11 +310,19 @@ when listing the current directory.

    $ go run directories.go
     Listing subdir/parent
    -child true
    -file2 false
    -file3 false
    +  child true
    +  file2 false
    +  file3 false
     Listing subdir/parent/child
    -file4 false
    +  file4 false
    +Visiting subdir
    +  subdir true
    +  subdir/file1 false
    +  subdir/parent true
    +  subdir/parent/child true
    +  subdir/parent/child/file4 false
    +  subdir/parent/file2 false
    +  subdir/parent/file3 false
     
    From 751eeda8b2117d0f7fd00c6444f25237694eab48 Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Sun, 30 Jun 2019 13:55:57 -0700 Subject: [PATCH 4/4] Code styling for symbol references --- examples/directories/directories.go | 4 ++-- examples/directories/directories.hash | 4 ++-- public/directories | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/examples/directories/directories.go b/examples/directories/directories.go index 8abdb51..9b71274 100644 --- a/examples/directories/directories.go +++ b/examples/directories/directories.go @@ -62,7 +62,7 @@ func main() { err = os.Chdir("subdir/parent/child") check(err) - // Now we'll see the contents of "subdir/parent/child" + // Now we'll see the contents of `subdir/parent/child` // when listing the *current* directory. c, err = ioutil.ReadDir(".") check(err) @@ -84,7 +84,7 @@ func main() { err = filepath.Walk("subdir", visit) } -// visit is called for every file or directory found +// `visit` is called for every file or directory found // recursively by `filepath.Walk`. func visit(p string, info os.FileInfo, err error) error { if err != nil { diff --git a/examples/directories/directories.hash b/examples/directories/directories.hash index 75cb9fa..cebae56 100644 --- a/examples/directories/directories.hash +++ b/examples/directories/directories.hash @@ -1,2 +1,2 @@ -57a8f629f040270c15a0375be4424b392edf3c95 -LI7ty_KDozd +83f67db91816b4544072d0a4d099111a21c60723 +-7kWq0PmATF diff --git a/public/directories b/public/directories index a1bfce5..e0746ba 100644 --- a/public/directories +++ b/public/directories @@ -28,7 +28,7 @@ - +
    package main
    @@ -218,7 +218,7 @@ similarly to cd.

    -

    Now we’ll see the contents of “subdir/parent/child” +

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

    @@ -280,7 +280,7 @@ directory visited.

    -

    visit is called for every file or directory found +

    visit is called for every file or directory found recursively by filepath.Walk.