From 56e1614c47cca7dc76b435f485fe86fe088d5127 Mon Sep 17 00:00:00 2001 From: Hasan Ozgan Date: Fri, 8 Feb 2019 00:50:51 +0000 Subject: [PATCH] path/filepath: add examples for Base, Dir and IsAbs Change-Id: I7a438409748f0f9d6517a7ea1cdee6512ce3ca8a Reviewed-on: https://go-review.googlesource.com/c/go/+/161678 Run-TryBot: Rob Pike Reviewed-by: Rob Pike --- src/path/filepath/example_unix_test.go | 71 ++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/path/filepath/example_unix_test.go b/src/path/filepath/example_unix_test.go index 20ec8927b4..23f21380d0 100644 --- a/src/path/filepath/example_unix_test.go +++ b/src/path/filepath/example_unix_test.go @@ -94,3 +94,74 @@ func ExampleMatch() { // true // true } + +func ExampleBase() { + fmt.Println("On Unix:") + fmt.Println(filepath.Base("/foo/bar/baz.js")) + fmt.Println(filepath.Base("/foo/bar/baz")) + fmt.Println(filepath.Base("/foo/bar/baz/")) + fmt.Println(filepath.Base("dev.txt")) + fmt.Println(filepath.Base("../todo.txt")) + fmt.Println(filepath.Base("..")) + fmt.Println(filepath.Base(".")) + fmt.Println(filepath.Base("/")) + fmt.Println(filepath.Base("")) + + // Output: + // On Unix: + // baz.js + // baz + // baz + // dev.txt + // todo.txt + // .. + // . + // / + // . +} + +func ExampleDir() { + fmt.Println("On Unix:") + fmt.Println(filepath.Dir("/foo/bar/baz.js")) + fmt.Println(filepath.Dir("/foo/bar/baz")) + fmt.Println(filepath.Dir("/foo/bar/baz/")) + fmt.Println(filepath.Dir("/dirty//path///")) + fmt.Println(filepath.Dir("dev.txt")) + fmt.Println(filepath.Dir("../todo.txt")) + fmt.Println(filepath.Dir("..")) + fmt.Println(filepath.Dir(".")) + fmt.Println(filepath.Dir("/")) + fmt.Println(filepath.Dir("")) + + // Output: + // On Unix: + // /foo/bar + // /foo/bar + // /foo/bar/baz + // /dirty/path + // . + // .. + // . + // . + // / + // . +} + +func ExampleIsAbs() { + fmt.Println("On Unix:") + fmt.Println(filepath.IsAbs("/home/gopher")) + fmt.Println(filepath.IsAbs(".bashrc")) + fmt.Println(filepath.IsAbs("..")) + fmt.Println(filepath.IsAbs(".")) + fmt.Println(filepath.IsAbs("/")) + fmt.Println(filepath.IsAbs("")) + + // Output: + // On Unix: + // true + // false + // false + // false + // true + // false +} -- 2.48.1