Skip to content

Commit 4dfa1d2

Browse files
committed
Add encoding/hex.Dumper example
1 parent c152965 commit 4dfa1d2

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

examples/basic/binaries/examples.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ func NewRegister() mappings.Register {
1818
func (r *register) Regist(m mappings.ExampleMapping) {
1919
m["binary_byteorder"] = ByteOrder
2020
m["binary_readwrite"] = readwrite.ReadWrite
21+
m["binary_using_hex_dumper"] = UsingHexDumper
2122
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package binaries
2+
3+
import (
4+
"encoding/hex"
5+
"io"
6+
"os"
7+
"strings"
8+
9+
"github.com/devlights/gomy/output"
10+
)
11+
12+
// UsingHexDumper -- encoding/hex#Dumper のサンプルです。
13+
func UsingHexDumper() error {
14+
// -----------------------------------------------------
15+
// encoding/hex#Dumper を利用すると hexdump コマンドを
16+
// 実行した場合のような16進数ダンプを出力することができる.
17+
//
18+
// hex.Dumper は、io.Writer を受け取り、io.WriteCloserを返す.
19+
// -----------------------------------------------------
20+
var (
21+
s string = "hello world"
22+
r io.Reader = strings.NewReader(s)
23+
w io.Writer = os.Stdout
24+
dumper io.WriteCloser = hex.Dumper(w)
25+
)
26+
27+
output.Stdoutl("[original]", s)
28+
29+
_, err := io.Copy(dumper, r)
30+
if err != nil {
31+
return err
32+
}
33+
34+
return nil
35+
}

0 commit comments

Comments
 (0)