-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexamples_test.go
70 lines (55 loc) · 1.21 KB
/
examples_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package stringz_test
import (
"fmt"
"io"
"io/ioutil"
"os"
"github.com/jzelinskie/stringz"
)
func ExampleTrimSurrounding() {
unquoted := stringz.TrimSurrounding(`"quoted text"`, `"`)
fmt.Println(unquoted)
// Output:
// quoted text
}
func ExampleDefaultEmpty() {
myFunc := func() string {
// Oh no! I failed!
// Return the empty string!
return ""
}
myFuncOutput := stringz.DefaultEmpty(myFunc(), "sane default")
fmt.Println(myFuncOutput)
// Output:
// sane default
}
func ExampleSliceMap() {
files := []string{"/home/my/file", "/home/your/file"}
if err := stringz.SliceMap(files, func(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close() // Defer runs for every iteration!
_, err = io.Copy(ioutil.Discard, f)
return err
}); err != nil {
fmt.Println(err)
}
}
func ExampleUnpack() {
var first, second string
if err := stringz.Unpack([]string{"hello", "world"}, &first, &second); err != nil {
panic(err)
}
fmt.Println(first, second)
// Output:
// hello world
}
func ExampleTrimPrefixIndex() {
fmt.Println(stringz.TrimPrefixIndex("this:that", ":"))
fmt.Println(stringz.TrimPrefixIndex("this::that", "::"))
// Output:
// that
// that
}