File tree Expand file tree Collapse file tree 2 files changed +44
-0
lines changed
Expand file tree Collapse file tree 2 files changed +44
-0
lines changed Original file line number Diff line number Diff line change @@ -18,4 +18,5 @@ func NewRegister() mappings.Register {
1818func (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}
Original file line number Diff line number Diff line change 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+ // 最後に Close を呼ばないと、ダンプ出力の右側に元の値が表示されないので注意
21+ // (Close を呼ばないままだと、16進部分のみが出力される)
22+ // -----------------------------------------------------
23+ var (
24+ s string = "hello world"
25+ r io.Reader = strings .NewReader (s )
26+ w io.Writer = os .Stdout
27+ dumper io.WriteCloser = hex .Dumper (w )
28+ )
29+
30+ defer func () {
31+ // Close を呼ぶことにより、出力の右側に値が出力される
32+ _ = dumper .Close ()
33+ }()
34+
35+ output .Stdoutl ("[original]" , s )
36+
37+ _ , err := io .Copy (dumper , r )
38+ if err != nil {
39+ return err
40+ }
41+
42+ return nil
43+ }
You can’t perform that action at this time.
0 commit comments