Golang笔记

golang snappy 示例

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

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))

}

输出:

source len: 454
compressed len: 223
uncompressed len: 454
本文网址: https://golangnote.com/topic/163.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。如果你有任何问题可以在下面 留言
Be the first to comment!
Captcha image
Relative Articles