GolangNote

Golang笔记

golang snappy 示例

Permalink

snappy 是 google 开源的一种高效的压缩算法,这里是 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

本文网址: https://golangnote.com/topic/163.html 转摘请注明来源

Related articles

golang snappy 的使用场合

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

Golang Web 程序生产环境独立部署示例

一个 web 应用通常是跑在一个前端代理,如 Nginx 后,这样可以方便的在同一个服务器部署多个应用。这里说的独立部署是指让 go web 程序直接暴露在外面,独占 443、80 端口(俗称裸跑)。这样做除了性能有些提高外,更重要的是部署方便。...

Golang phantomjs 动态代理实现

phantomjs 是个很优秀的软件,虽然现在被chrome headless 抢了风头,但在某些特定场合,使用phantomjs 还是很方便,这里是介绍使用Go 实现动态代理。...

Golang 生成防识别的图片验证码

验证码 captcha 是对抗密码强力破解、垃圾信息的有效方式,一般用于用户注册、登录,当检测到频繁发帖时也会启用验证码。下面介绍用golang 生成防机器识别的图片验证码。...

bolt 使用示例

bolt 是一款高性能的key value 数据库,下面是它的使用示例:...

Write a Comment to "golang snappy 示例"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.22.3 Processed in 0ms