Skip to content

Commit 200ec31

Browse files
committed
Add examples/basic/fileio/multiwriter/gzip_and_crc.go
1 parent d13503d commit 200ec31

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

examples/basic/fileio/examples.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package fileio
33
import (
44
"github.com/devlights/try-golang/examples/basic/fileio/filesystem"
55
"github.com/devlights/try-golang/examples/basic/fileio/ja"
6+
"github.com/devlights/try-golang/examples/basic/fileio/multiwriter"
67
"github.com/devlights/try-golang/examples/basic/fileio/readwrite"
78
"github.com/devlights/try-golang/examples/basic/fileio/stat"
89
"github.com/devlights/try-golang/examples/basic/fileio/stdinouterr"
@@ -27,4 +28,5 @@ func (r *register) Regist(m mapping.ExampleMapping) {
2728
filesystem.NewRegister().Regist(m)
2829
ja.NewRegister().Regist(m)
2930
sync_and_close.NewRegister().Regist(m)
31+
multiwriter.NewRegister().Regist(m)
3032
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# サンプルリスト
2+
3+
このディレクトリには以下のサンプルがあります。
4+
5+
| file | example name | note |
6+
| --------------- | ----------------------------- | ------------------------------------------------------------------------------ |
7+
| gzip_and_crc.go | fileio_multiwriter_gzipandcrc | io.MultiWriterを利用してgzip圧縮しながらCRCチェックサムも算出するサンプルです. |
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package multiwriter
2+
3+
import "github.com/devlights/try-golang/mapping"
4+
5+
type (
6+
register struct{}
7+
)
8+
9+
var (
10+
_ mapping.Register = (*register)(nil)
11+
)
12+
13+
// NewRegister -- このパッケージ用のサンプルを登録する mapping.Register を生成します。
14+
func NewRegister() mapping.Register {
15+
return new(register)
16+
}
17+
18+
// Regist -- 登録します.
19+
func (r *register) Regist(m mapping.ExampleMapping) {
20+
m["fileio_multiwriter_gzipandcrc"] = GzipAndCrc
21+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package multiwriter
2+
3+
import (
4+
"bytes"
5+
"compress/gzip"
6+
"encoding/hex"
7+
"hash/crc32"
8+
"io"
9+
"os"
10+
11+
"github.com/devlights/gomy/output"
12+
)
13+
14+
// GzipAndCrc は、io.MultiWriterを利用してgzip圧縮しながらCRCチェックサムも算出するサンプルです.
15+
//
16+
// # REFERENCES
17+
//
18+
// - https://pkg.go.dev/io@go1.22.2#MultiWriter
19+
// - https://pkg.go.dev/hash/crc32@go1.22.2#NewIEEE
20+
// - https://pkg.go.dev/compress/gzip@go1.22.2#Writer
21+
// - https://pkg.go.dev/encoding/hex@go1.22.2#Dumper
22+
func GzipAndCrc() error {
23+
var (
24+
data = []byte("hello world こんにちは 世界")
25+
buf = new(bytes.Buffer)
26+
gzipW = gzip.NewWriter(buf)
27+
crcW = crc32.NewIEEE()
28+
writer = io.MultiWriter(gzipW, crcW, hex.Dumper(os.Stdout))
29+
err error
30+
)
31+
defer gzipW.Close()
32+
33+
_, err = writer.Write(data)
34+
if err != nil {
35+
return err
36+
}
37+
38+
output.Stdoutl("")
39+
output.Stdoutf("[orig]", "%x\n", data)
40+
output.Stdoutf("[gzip]", "%x\n", buf.Bytes())
41+
output.Stdoutf("[crc ]", "%x\n", crcW.Sum32())
42+
43+
return nil
44+
45+
/*
46+
$ task
47+
task: [build] go build .
48+
task: [run] ./try-golang -onetime
49+
50+
ENTER EXAMPLE NAME: fileio_multiwriter
51+
52+
[Name] "fileio_multiwriter_gzipandcrc"
53+
00000000 68 65 6c 6c 6f 20 77 6f 72 6c 64 20 e3 81 93 e3 |hello world ....|
54+
00000010 82 93 e3 81 ab e3 81 a1 e3 81 af 20 e4 b8 96 e7 |........... ....|
55+
00000020 95 8c
56+
[orig] 68656c6c6f20776f726c6420e38193e38293e381abe381a1e381af20e4b896e7958c
57+
[gzip] 1f8b08000000000000ff
58+
[crc ] 6535a281
59+
60+
61+
[Elapsed] 343.1µs
62+
*/
63+
64+
}

0 commit comments

Comments
 (0)