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 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 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 |
+
+
+ err = os.MkdirAll("subdir/parent/child", 0755)
+ check(err)
+ |
+
+ + | +
+
+ createEmptyFile("subdir/parent/file2")
+ createEmptyFile("subdir/parent/file3")
+ createEmptyFile("subdir/parent/child/file4")
+ |
+
+
|
+
+
+ c, err := ioutil.ReadDir("subdir/parent")
+ check(err)
+ |
+
+ + | +
+
+ fmt.Println("Listing subdir/parent")
+ for _, entry := range c {
+ fmt.Println(entry.Name(), entry.IsDir())
+ }
+ |
+
+
|
+
+
+ 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())
+ }
+ |
+
+
|
+
+
+ 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. +
+ + +- Next example: Command-Line Arguments. + Next example: Directories.