diff --git a/examples.txt b/examples.txt index d4859d3..91a1fde 100644 --- a/examples.txt +++ b/examples.txt @@ -57,6 +57,7 @@ Base64 Encoding Reading Files Writing Files Line Filters +File Paths Command-Line Arguments Command-Line Flags Command-Line Subcommands diff --git a/examples/file-paths/file-paths.go b/examples/file-paths/file-paths.go new file mode 100644 index 0000000..75b017d --- /dev/null +++ b/examples/file-paths/file-paths.go @@ -0,0 +1,62 @@ +// The `filepath` package provides functions to parse +// and construct *file paths* in a way that is portable +// between operating systems; `dir/file` on Linux vs. +// `dir\file` on Windows, for example. +package main + +import ( + "fmt" + "path/filepath" + "strings" +) + +func main() { + + // `Join` should be used to construct paths in a + // portable way. It takes any number of arguments + // and constructs a hierarchical path from them. + p := filepath.Join("dir1", "dir2", "filename") + fmt.Println("p:", p) + + // You should always use `Join` instead of + // concatenating `/`s or `\`s manually. In addition + // to providing portability, `Join` will also + // normalize paths by removing superfluous separators + // and directory changes. + fmt.Println(filepath.Join("dir1//", "filename")) + fmt.Println(filepath.Join("dir1/../dir1", "filename")) + + // `Dir` and `Base` can be used to split a path to the + // directory and the file. Alternatively, `Split` will + // return both in the same call. + fmt.Println("Dir(p):", filepath.Dir(p)) + fmt.Println("Base(p):", filepath.Base(p)) + + // To check whether a path is absolute, use `IsAbs`. + fmt.Println(filepath.IsAbs("dir/file")) + fmt.Println(filepath.IsAbs("/dir/file")) + + filename := "config.json" + + // To find a file's extension, use `Ext`. + ext := filepath.Ext(filename) + fmt.Println(ext) + + // To find the file's name with the extension removed, + // use `TrimSuffix`. + fmt.Println(strings.TrimSuffix(filename, ext)) + + // `Rel` finds a relative path between a *base* and a + // *target*. + rel, err := filepath.Rel("a/b", "a/b/t/file") + if err != nil { + panic(err) + } + fmt.Println(rel) + + rel, err = filepath.Rel("a/b", "a/c/t/file") + if err != nil { + panic(err) + } + fmt.Println(rel) +} diff --git a/examples/file-paths/file-paths.hash b/examples/file-paths/file-paths.hash new file mode 100644 index 0000000..7cc06ab --- /dev/null +++ b/examples/file-paths/file-paths.hash @@ -0,0 +1,2 @@ +4611ff69626490eb50673a739707d870fac79142 +eUhAltl7_sI diff --git a/examples/file-paths/file-paths.sh b/examples/file-paths/file-paths.sh new file mode 100644 index 0000000..4ef4251 --- /dev/null +++ b/examples/file-paths/file-paths.sh @@ -0,0 +1,12 @@ +$ go run file-paths.go +p: dir1/dir2/filename +dir1/filename +dir1/filename +Dir(p): dir1/dir2 +Base(p): filename +false +true +.json +config +t/file +../c/t/file diff --git a/public/file-paths b/public/file-paths new file mode 100644 index 0000000..9085662 --- /dev/null +++ b/public/file-paths @@ -0,0 +1,238 @@ + + +
+ +
+ The |
+
+
+ package main
+ |
+
+ + | +
+
+ import (
+ "fmt"
+ "path/filepath"
+ "strings"
+)
+ |
+
+ + | +
+
+ func main() {
+ |
+
+
|
+
+
+ p := filepath.Join("dir1", "dir2", "filename")
+ fmt.Println("p:", p)
+ |
+
+ You should always use |
+
+
+ fmt.Println(filepath.Join("dir1//", "filename"))
+ fmt.Println(filepath.Join("dir1/../dir1", "filename"))
+ |
+
+
|
+
+
+ fmt.Println("Dir(p):", filepath.Dir(p))
+ fmt.Println("Base(p):", filepath.Base(p))
+ |
+
+ To check whether a path is absolute, use |
+
+
+ fmt.Println(filepath.IsAbs("dir/file"))
+ fmt.Println(filepath.IsAbs("/dir/file"))
+ |
+
+ + | +
+
+ filename := "config.json"
+ |
+
+ To find a file’s extension, use |
+
+
+ ext := filepath.Ext(filename)
+ fmt.Println(ext)
+ |
+
+ To find the file’s name with the extension removed,
+use |
+
+
+ fmt.Println(strings.TrimSuffix(filename, ext))
+ |
+
+
|
+
+
+ rel, err := filepath.Rel("a/b", "a/b/t/file")
+ if err != nil {
+ panic(err)
+ }
+ fmt.Println(rel)
+ |
+
+ + | +
+
+ rel, err = filepath.Rel("a/b", "a/c/t/file")
+ if err != nil {
+ panic(err)
+ }
+ fmt.Println(rel)
+}
+ |
+
+ + | +
+
+ $ go run file-paths.go
+p: dir1/dir2/filename
+dir1/filename
+dir1/filename
+Dir(p): dir1/dir2
+Base(p): filename
+false
+true
+.json
+config
+t/file
+../c/t/file
+ |
+
+ Next example: Command-Line Arguments. +
+ + +- Next example: Command-Line Arguments. + Next example: File Paths.