Source file
src/encoding/hex/example_test.go
1
2
3
4
5 package hex_test
6
7 import (
8 "encoding/hex"
9 "fmt"
10 "log"
11 "os"
12 )
13
14 func ExampleEncode() {
15 src := []byte("Hello Gopher!")
16
17 dst := make([]byte, hex.EncodedLen(len(src)))
18 hex.Encode(dst, src)
19
20 fmt.Printf("%s\n", dst)
21
22
23
24 }
25
26 func ExampleDecode() {
27 src := []byte("48656c6c6f20476f7068657221")
28
29 dst := make([]byte, hex.DecodedLen(len(src)))
30 n, err := hex.Decode(dst, src)
31 if err != nil {
32 log.Fatal(err)
33 }
34
35 fmt.Printf("%s\n", dst[:n])
36
37
38
39 }
40
41 func ExampleDecodeString() {
42 const s = "48656c6c6f20476f7068657221"
43 decoded, err := hex.DecodeString(s)
44 if err != nil {
45 log.Fatal(err)
46 }
47
48 fmt.Printf("%s\n", decoded)
49
50
51
52 }
53
54 func ExampleDump() {
55 content := []byte("Go is an open source programming language.")
56
57 fmt.Printf("%s", hex.Dump(content))
58
59
60
61
62
63 }
64
65 func ExampleDumper() {
66 lines := []string{
67 "Go is an open source programming language.",
68 "\n",
69 "We encourage all Go users to subscribe to golang-announce.",
70 }
71
72 stdoutDumper := hex.Dumper(os.Stdout)
73
74 defer stdoutDumper.Close()
75
76 for _, line := range lines {
77 stdoutDumper.Write([]byte(line))
78 }
79
80
81
82
83
84
85
86
87
88 }
89
90 func ExampleEncodeToString() {
91 src := []byte("Hello")
92 encodedStr := hex.EncodeToString(src)
93
94 fmt.Printf("%s\n", encodedStr)
95
96
97
98 }
99
View as plain text