This repository was archived by the owner on Aug 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_test.go
More file actions
112 lines (85 loc) · 2.11 KB
/
example_test.go
File metadata and controls
112 lines (85 loc) · 2.11 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package genrawid_test
import (
"fmt"
"log"
"os"
"github.com/KEINOS/go-genrawid"
)
// ----------------------------------------------------------------------------
// Examples for public functions
// ----------------------------------------------------------------------------
func ExampleFromString() {
input := "abcdefgh"
rawid, err := genrawid.FromString(input)
if err != nil {
log.Fatal(err)
}
fmt.Println(rawid.Hex())
fmt.Println(rawid.Dec())
// Output:
// ddaa2ac39b79058a
// -2474118025671277174
}
func ExampleFromFile() {
const pathFile = "./testdata/msg.txt" // msg.txt ==> "abcdefgh"
rawid, err := genrawid.FromFile(pathFile)
if err != nil {
log.Fatal(err)
}
fmt.Println(rawid.Hex())
fmt.Println(rawid.Dec())
// Output:
// ddaa2ac39b79058a
// -2474118025671277174
}
func ExampleFromFile_fast_mode() {
oldMode := genrawid.IsModeFast
defer func() {
genrawid.IsModeFast = oldMode
}()
// Set to fast mode
genrawid.IsModeFast = true
pathFile := "./testdata/msg.txt" // msg.txt ==> "abcdefgh"
rawid, err := genrawid.FromFile(pathFile)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(rawid.Hex())
fmt.Println(rawid.Dec())
// Output:
// ddaa2ac30a98963b
// -2474118028101904837
}
// ExampleFromStdin
//
// Since we can not receive input from stdin during the example run, we mock
// the OsStdin, the copy of os.Stdin in the package, by assigning a dummy
// file as its input.
func ExampleFromStdin() {
// Backup and defer recover the stdin
oldStdin := genrawid.OsStdin
defer func() {
genrawid.OsStdin = oldStdin
}()
// Mock the stdin by assigning a file to stdin.
pathFile := "./testdata/msg.txt" // msg.txt ==> "abcdefgh"
osFile, err := os.Open(pathFile)
if err != nil {
fmt.Println(err.Error())
return
}
genrawid.OsStdin = osFile // <-- mock!
// Here is the actual example usage of FromStdin method to get the input from
// stdin and generate a rawid.
rawid, err := genrawid.FromStdin()
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Println(rawid.Hex())
fmt.Println(rawid.Dec())
// Output:
// ddaa2ac39b79058a
// -2474118025671277174
}