GolangNote

Golang笔记

golang snappy 示例

Permalink

snappy 是 google 开源的一种高效的压缩算法,这里是 golang 实现的 snappy 压缩示例

golang snappy 示例

Go: snappy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package main

import (
	"fmt"

	"github.com/golang/snappy"
)

const test = `{"Ag(T+D)":{"instID":"Ag(T+D)","name":"白银延期","last":"4141","upDown":"21","upDownRate":"0.51","quoteDate":"20170328","quoteTime":"22:34:29"},"Au(T+D)":{"instID":"Au(T+D)","name":"黄金延期","last":"280.55","upDown":"0.88","upDownRate":"0.31","quoteDate":"20170328","quoteTime":"22:34:15"},"mAu(T+D)":{"instID":"mAu(T+D)","name":"Mini黄金延期","last":"280.5","upDown":"0.7","upDownRate":"0.25","quoteDate":"20170328","quoteTime":"22:34:10"}}`

func main() {

	fmt.Println("source len:", len(test))

	got := snappy.Encode(nil, []byte(test))
	fmt.Println("compressed len:", len(got))

	a, _ := snappy.Decode(nil, got)
	fmt.Println("uncompressed len:", len(a))

}

输出:

plaintext: snappy
1
2
3
source len: 454
compressed len: 223
uncompressed len: 454

Related articles

golang snappy 的使用场合

google 自家的 snappy 压缩优点是非常高的速度和合理的压缩率。压缩率比 gzip 小,CPU 占用小。...

Golang sync.WaitGroup 的 Wait 超时处理

sync.WaitGroup 使用 `Add(1)`、`Done()`、`Wait()`组合来实现多协程等待,如果某一协程未能合理处理错误,导致无法退出,此时需要引入超时机制。下面是一种超时处理方法。...

Write a Comment to "golang snappy 示例"

Submit Comment Login
Based on Golang + fastHTTP + youdb | go1.16 Processed in 0ms