Golang笔记

Golang 用snappy + Base64 简单压缩加密进行网络传输

snappy 是较省CPU 的压缩算法,速度快,可以用于简单数据压缩,结合Base64 进行网络传输。

package main

import (
	"encoding/base64"
	"fmt"
	"github.com/golang/snappy"
	"strings"
)

const (
	s = `{"130":[0,0,0,0,0,0,1,0,0,0,0,0],"139":[1,0,0,0,0,0,0,0,0,0,0,0],"140":[0,0,0,1,0,0,0,0,0,0,0,0],"141":[0,0,0,0,0,1,0,0,0,0,0,0],"143":[0,1,0,0,0,0,0,0,0,0,0,0],"144":[0,1,1,0,1,1,1,0,0,0,0,0],"145":[1,0,0,0,0,0,0,0,0,1,0,0],"146":[1,0,0,0,0,0,1,0,0,1,0,1],"147":[1,0,1,0,0,0,2,0,1,2,3,1],"148":[0,0,0,0,2,0,0,1,0,1,0,2],"149":[1,0,1,0,5,4,4,2,0,2,0,1],"150":[0,1,2,1,1,1,0,1,2,5,0,3],"151":[1,4,2,3,1,2,2,1,4,2,2,3],"152":[4,8,1,3,3,2,2,1,5,7,2,1],"153":[10,5,7,8,8,9,6,6,3,2,9,4],"154":[6,8,6,9,11,10,7,5,7,10,9,8],"155":[8,15,6,13,11,6,5,9,8,8,8,7],"156":[9,16,12,21,12,19,14,17,13,11,13,9],"157":[24,19,10,16,25,19,21,20,19,17,16,16],"158":[22,24,33,24,31,29,33,23,22,32,25,25],"159":[31,21,40,29,31,29,34,36,29,41,33,36],"160":[31,39,33,56,33,34,52,34,46,57,51,47],"161":[49,74,60,77,56,76,64,65,57,70,59,65],"162":[95,62,81,70,79,69,50,70,81,90,76,77],"163":[91,107,93,96,115,104,85,97,86,81,104,98],"164":[125,140,134,136,133,122,129,135,128,118,128,139],"165":[158,177,144,177,158,176,168,161,165,162,158,192],"166":[208,196,196,240,199,199,231,200,196,193,218,216],"167":[270,291,243,267,250,255,289,256,249,282,277,289],"168":[322,339,333,333,327,326,331,352,338,344,359,334],"169":[446,421,417,462,397,403,433,388,417,406,424,392],"170":[508,495,507,521,481,501,536,541,509,541,491,554],"171":[673,652,638,637,661,651,644,614,629,621,642,610],"172":[823,748,757,781,769,759,799,771,785,795,755,785],"173":[917,969,977,942,979,1017,996,951,1000,994,971,947],"174":[1162,1265,1219,1096,1143,1240,1214,1179,1117,1164,1138,1156],"175":[1415,1440,1430,1319,1413,1391,1432,1403,1364,1464,1368,1396],"176":[1682,1786,1744,1732,1633,1676,1617,1696,1691,1700,1743,1673],"177":[2014,1959,2002,2027,1998,1996,1998,2017,1984,1973,1991,1985],"178":[2362,2397,2241,2347,2334,2371,2254,2397,2354,2310,2385,2348],"179":[2740,2811,2742,2837,2747,2720,2833,2736,2704,2810,2782,2782],"180":[3063,3203,3249,3203,3209,3236,3194,3260,3286,3235,3298,3301],"181":[3766,3701,3874,3789,3697,3672,3729,3777,3776,3721,3866,3747],"182":[4398,4357,4362,4298,4191,4264,4335,4388,4296,4331,4231,4271],"183":[4928,4960,4940,5010,5002,4923,4926,4907,4857,4931,4866,5049],"184":[5652,5660,5446,5522,5711,5617,5766,5621,5664,5608,5664,5516],"185":[6443,6406,6414,6615,6540,6456,6376,6269,6434,6441,6567,6244],"186":[7398,7227,7192,7006,7184,7018,7219,7323,7124,7297,7255,7203],"187":[7958,8121,8078,8152,7987,8000,8128,8029,8071,8126,8050,8166],"188":[9016,8877,8928,8769,9032,9042,9034,9030,8964,8950,9078,8918],"189":[10138,10081,10049,10102,9899,10070,9914,10052,10176,10050,9976,10102],"190":[10942,10969,11002,11096,11033,11129,10962,10715,10931,10985,11269,11087],"191":[12127,11949,11867,11914,12172,12069,11997,11984,12196,11971,12138,12005],"192":[13221,13058,13118,13110,12972,13011,13037,13079,12970,13087,13057,13053],"193":[14100,14241,14005,14212,14085,14026,14271,14156,14154,14180,14128,14030],"194":[15195,15095,15200,15201,15237,15179,15208,15098,15304,15131,15442,15033],"195":[16444,16259,16439,16208,16599,16381,16282,16163,16277,16244,16323,16126],"196":[17366,17388,17509,17464,17319,17581,17318,17228,17629,17221,17334,17339],"197":[18465,18335,18320,18416,18488,18232,18250,18451,18325,18105,18291,18403],"198":[19211,19458,19233,19400,19771,19375,19333,19006,19123,19189,19460,19369],"199":[20206,20199,20096,20636,20150,20143,20230,20190,20544,20149,19957,20163],"200":[21005,21249,21255,21052,20871,21055,21346,21327,21023,21083,20982,21156],"201":[21907,21952,21808,21615,21521,22140,21815,21603,21940,21913,21918,21997],"202":[22684,22456,22316,22874,22507,22336,22583,23013,22606,22597,22668,22409],"203":[23150,22902,22883,22889,23185,22962,23137,22973,23112,23090,23258,22971],"204":[23346,23480,23547,23617,23735,23655,23674,23776,23265,23495,23444,23449],"205":[24000,23684,24030,23866,23611,23767,23615,23850,23648,23823,23824,23810],"206":[24382,24002,24092,23721,23730,23867,24020,23824,23993,24329,23839,24083],"207":[24152,24477,24132,24422,24237,23942,24228,24011,24246,24038,24145,23693],"208":[23935,23961,24009,24236,23977,23770,24014,23878,24551,24125,24257,23930],"209":[23899,24216,23716,24033,23763,23959,23775,23877,24052,23796,23787,23696],"210":[23551,23363,23612,23494,23577,23481,23362,23792,23738,23394,23520,23650],"211":[23134,23300,22961,23466,23508,23169,23053,23274,23255,23392,23046,23060],"212":[22529,22717,22804,22679,22780,22524,22687,22902,22485,22736,22634,22673],"213":[21973,22031,21825,21792,21961,21977,21624,21951,21872,21939,21871,22260],"214":[21486,21152,21250,21268,21610,21403,21248,21163,21189,21404,21430,21443],"215":[20408,20241,20634,20285,20418,20604,20427,20834,20591,20379,20331,20643],"216":[19481,19960,19677,19589,19632,19453,19555,19591,19787,19609,19686,19425],"217":[18854,18729,18548,18837,18652,18835,18396,18661,18645,18870,18490,18711],"218":[17617,17604,17655,17810,17798,17589,17522,17636,17765,17698,17631,17750],"219":[16836,16710,16915,16808,16651,16720,16599,16597,16690,16728,16644,16698],"220":[15513,15725,15530,15752,15782,15755,15617,15674,15952,15578,15681,15720],"221":[14401,14471,14562,14466,14525,14496,14898,14633,15022,14440,14682,14526],"222":[13473,13518,13753,13561,13602,13640,13541,13612,13676,13575,13707,13686],"223":[12645,12704,12537,12636,12771,12536,12564,12643,12710,12794,12660,12535],"224":[11755,11700,11575,11657,11481,11448,11591,11547,11438,11648,11707,11473],"225":[10639,10612,10529,10628,10538,10554,10570,10561,10528,10572,10569,10666],"226":[9701,9535,9613,9715,9542,9593,9743,9505,9713,9764,9674,9730],"227":[8706,8857,8690,9020,8826,8816,8521,8811,8679,8842,8857,8776],"228":[8073,7913,8009,7909,7748,7998,7802,7974,7806,7983,8020,7775],"229":[6977,7022,7013,7223,7156,6970,7054,7002,7038,7030,7116,7191],"230":[6233,6342,6293,6370,6403,6305,6303,6330,6270,6310,6371,6264],"231":[5608,5662,5591,5648,5642,5708,5541,5655,5602,5646,5719,5565],"232":[5055,4900,4934,4996,5101,5015,5089,4874,4948,4887,4882,4895],"233":[4281,4414,4426,4413,4382,4348,4385,4379,4466,4417,4308,4405],"234":[3949,3832,3792,3794,3851,3815,3914,3842,3866,3924,3783,3794],"235":[3399,3315,3372,3336,3344,3449,3325,3309,3445,3359,3403,3445],"236":[2916,2949,3046,2987,2998,2916,2953,2910,3017,2881,2915,2885],"237":[2469,2553,2607,2591,2593,2554,2549,2530,2527,2536,2473,2517],"238":[2283,2125,2082,2147,2189,2246,2135,2208,2122,2224,2222,2123],"239":[1840,1834,1939,1932,1853,1821,1886,1860,1872,1819,1951,1889],"240":[1551,1592,1586,1560,1562,1589,1605,1600,1643,1590,1621,1700],"241":[1323,1374,1404,1315,1341,1283,1345,1345,1253,1387,1380,1366],"242":[1100,1149,1117,1100,1122,1131,1106,1137,1116,1094,1154,1137],"243":[937,921,924,996,949,950,969,1003,970,946,925,1070],"244":[836,841,804,774,810,815,759,753,779,786,784,803],"245":[643,734,660,694,662,682,681,678,659,675,658,677],"246":[571,532,584,517,577,571,558,592,591,548,551,517],"247":[443,419,458,406,459,476,471,488,458,435,461,464],"248":[394,356,396,393,404,395,383,367,367,387,342,393],"249":[315,300,343,323,285,280,316,290,348,304,286,336],"250":[252,260,250,248,245,251,270,252,257,239,239,256],"251":[184,219,191,198,189,203,202,210,204,196,211,188],"252":[164,155,159,167,178,154,172,159,160,154,153,149],"253":[129,126,152,150,129,111,135,117,103,122,149,143],"254":[103,124,105,126,129,90,117,86,77,119,109,114],"255":[84,76,82,98,70,81,91,85,84,66,66,61],"256":[85,69,69,73,69,73,64,58,74,69,74,71],"257":[56,51,60,65,72,49,46,54,52,51,52,63],"258":[42,50,43,46,47,48,46,42,50,40,42,24],"259":[35,38,39,40,28,29,35,24,26,24,29,35],"260":[25,27,25,31,22,29,30,29,40,24,29,25],"261":[14,17,17,25,19,23,20,23,24,19,13,23],"262":[9,16,15,13,21,18,16,10,12,21,19,16],"263":[10,11,13,10,9,21,11,7,10,7,6,10],"264":[4,7,7,11,10,6,5,9,10,11,9,9],"265":[4,8,5,5,8,4,11,4,3,14,8,9],"266":[6,5,4,3,8,9,7,6,5,6,3,2],"267":[7,6,7,3,2,2,3,1,1,5,4,4],"268":[4,2,1,2,0,3,2,3,2,4,6,2],"269":[0,1,2,5,4,1,2,3,1,2,3,4],"270":[1,0,0,3,2,1,2,7,6,1,3,2],"271":[1,0,0,0,2,0,1,0,0,2,1,2],"272":[1,1,0,0,2,1,2,1,1,0,0,0],"273":[0,3,1,1,0,0,1,0,1,0,1,1],"274":[0,1,2,1,1,0,0,0,0,0,0,0],"275":[2,0,0,0,1,0,0,0,0,0,0,0],"276":[0,0,2,0,0,0,0,1,0,0,0,0],"277":[0,0,0,0,1,0,0,0,1,0,0,0],"278":[0,2,0,0,0,0,0,0,0,0,0,0],"279":[0,0,0,0,0,0,0,2,0,0,0,0],"281":[1,0,0,0,0,1,0,0,0,0,0,0]}`
)

func snappyBase64Encode(src string) string {
	got := snappy.Encode(nil, []byte(src))
	return base64.StdEncoding.EncodeToString(got)
}

func snappyBase64Decode(src string) (out string, err error) {
	var a []byte

	a, err = base64.StdEncoding.DecodeString(src)
	if err != nil {
		return
	}

	a, err = snappy.Decode(nil, a)
	if err != nil {
		return
	}

	out = string(a)
	return
}

func main() {
	enStr := snappyBase64Encode(s)
	deStr, err := snappyBase64Decode(enStr)
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(strings.EqualFold(s, deStr))
	fmt.Println("s len", len(s), "enStr len", len(enStr))
}

输出:

true
s len 8213 enStr len 8320

实际使用一般都用[]byte,下面是byte版:

package main

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"github.com/golang/snappy"
)

const (
	s = `{"130":[0,0,0,0,0,0,1,0,0,0,0,0],"139":[1,0,0,0,0,0,0,0,0,0,0,0],"140":[0,0,0,1,0,0,0,0,0,0,0,0],"141":[0,0,0,0,0,1,0,0,0,0,0,0],"143":[0,1,0,0,0,0,0,0,0,0,0,0],"144":[0,1,1,0,1,1,1,0,0,0,0,0],"145":[1,0,0,0,0,0,0,0,0,1,0,0],"146":[1,0,0,0,0,0,1,0,0,1,0,1],"147":[1,0,1,0,0,0,2,0,1,2,3,1],"148":[0,0,0,0,2,0,0,1,0,1,0,2],"149":[1,0,1,0,5,4,4,2,0,2,0,1],"150":[0,1,2,1,1,1,0,1,2,5,0,3],"151":[1,4,2,3,1,2,2,1,4,2,2,3],"152":[4,8,1,3,3,2,2,1,5,7,2,1],"153":[10,5,7,8,8,9,6,6,3,2,9,4],"154":[6,8,6,9,11,10,7,5,7,10,9,8],"155":[8,15,6,13,11,6,5,9,8,8,8,7],"156":[9,16,12,21,12,19,14,17,13,11,13,9],"157":[24,19,10,16,25,19,21,20,19,17,16,16],"158":[22,24,33,24,31,29,33,23,22,32,25,25],"159":[31,21,40,29,31,29,34,36,29,41,33,36],"160":[31,39,33,56,33,34,52,34,46,57,51,47],"161":[49,74,60,77,56,76,64,65,57,70,59,65],"162":[95,62,81,70,79,69,50,70,81,90,76,77],"163":[91,107,93,96,115,104,85,97,86,81,104,98],"164":[125,140,134,136,133,122,129,135,128,118,128,139],"165":[158,177,144,177,158,176,168,161,165,162,158,192],"166":[208,196,196,240,199,199,231,200,196,193,218,216],"167":[270,291,243,267,250,255,289,256,249,282,277,289],"168":[322,339,333,333,327,326,331,352,338,344,359,334],"169":[446,421,417,462,397,403,433,388,417,406,424,392],"170":[508,495,507,521,481,501,536,541,509,541,491,554],"171":[673,652,638,637,661,651,644,614,629,621,642,610],"172":[823,748,757,781,769,759,799,771,785,795,755,785],"173":[917,969,977,942,979,1017,996,951,1000,994,971,947],"174":[1162,1265,1219,1096,1143,1240,1214,1179,1117,1164,1138,1156],"175":[1415,1440,1430,1319,1413,1391,1432,1403,1364,1464,1368,1396],"176":[1682,1786,1744,1732,1633,1676,1617,1696,1691,1700,1743,1673],"177":[2014,1959,2002,2027,1998,1996,1998,2017,1984,1973,1991,1985],"178":[2362,2397,2241,2347,2334,2371,2254,2397,2354,2310,2385,2348],"179":[2740,2811,2742,2837,2747,2720,2833,2736,2704,2810,2782,2782],"180":[3063,3203,3249,3203,3209,3236,3194,3260,3286,3235,3298,3301],"181":[3766,3701,3874,3789,3697,3672,3729,3777,3776,3721,3866,3747],"182":[4398,4357,4362,4298,4191,4264,4335,4388,4296,4331,4231,4271],"183":[4928,4960,4940,5010,5002,4923,4926,4907,4857,4931,4866,5049],"184":[5652,5660,5446,5522,5711,5617,5766,5621,5664,5608,5664,5516],"185":[6443,6406,6414,6615,6540,6456,6376,6269,6434,6441,6567,6244],"186":[7398,7227,7192,7006,7184,7018,7219,7323,7124,7297,7255,7203],"187":[7958,8121,8078,8152,7987,8000,8128,8029,8071,8126,8050,8166],"188":[9016,8877,8928,8769,9032,9042,9034,9030,8964,8950,9078,8918],"189":[10138,10081,10049,10102,9899,10070,9914,10052,10176,10050,9976,10102],"190":[10942,10969,11002,11096,11033,11129,10962,10715,10931,10985,11269,11087],"191":[12127,11949,11867,11914,12172,12069,11997,11984,12196,11971,12138,12005],"192":[13221,13058,13118,13110,12972,13011,13037,13079,12970,13087,13057,13053],"193":[14100,14241,14005,14212,14085,14026,14271,14156,14154,14180,14128,14030],"194":[15195,15095,15200,15201,15237,15179,15208,15098,15304,15131,15442,15033],"195":[16444,16259,16439,16208,16599,16381,16282,16163,16277,16244,16323,16126],"196":[17366,17388,17509,17464,17319,17581,17318,17228,17629,17221,17334,17339],"197":[18465,18335,18320,18416,18488,18232,18250,18451,18325,18105,18291,18403],"198":[19211,19458,19233,19400,19771,19375,19333,19006,19123,19189,19460,19369],"199":[20206,20199,20096,20636,20150,20143,20230,20190,20544,20149,19957,20163],"200":[21005,21249,21255,21052,20871,21055,21346,21327,21023,21083,20982,21156],"201":[21907,21952,21808,21615,21521,22140,21815,21603,21940,21913,21918,21997],"202":[22684,22456,22316,22874,22507,22336,22583,23013,22606,22597,22668,22409],"203":[23150,22902,22883,22889,23185,22962,23137,22973,23112,23090,23258,22971],"204":[23346,23480,23547,23617,23735,23655,23674,23776,23265,23495,23444,23449],"205":[24000,23684,24030,23866,23611,23767,23615,23850,23648,23823,23824,23810],"206":[24382,24002,24092,23721,23730,23867,24020,23824,23993,24329,23839,24083],"207":[24152,24477,24132,24422,24237,23942,24228,24011,24246,24038,24145,23693],"208":[23935,23961,24009,24236,23977,23770,24014,23878,24551,24125,24257,23930],"209":[23899,24216,23716,24033,23763,23959,23775,23877,24052,23796,23787,23696],"210":[23551,23363,23612,23494,23577,23481,23362,23792,23738,23394,23520,23650],"211":[23134,23300,22961,23466,23508,23169,23053,23274,23255,23392,23046,23060],"212":[22529,22717,22804,22679,22780,22524,22687,22902,22485,22736,22634,22673],"213":[21973,22031,21825,21792,21961,21977,21624,21951,21872,21939,21871,22260],"214":[21486,21152,21250,21268,21610,21403,21248,21163,21189,21404,21430,21443],"215":[20408,20241,20634,20285,20418,20604,20427,20834,20591,20379,20331,20643],"216":[19481,19960,19677,19589,19632,19453,19555,19591,19787,19609,19686,19425],"217":[18854,18729,18548,18837,18652,18835,18396,18661,18645,18870,18490,18711],"218":[17617,17604,17655,17810,17798,17589,17522,17636,17765,17698,17631,17750],"219":[16836,16710,16915,16808,16651,16720,16599,16597,16690,16728,16644,16698],"220":[15513,15725,15530,15752,15782,15755,15617,15674,15952,15578,15681,15720],"221":[14401,14471,14562,14466,14525,14496,14898,14633,15022,14440,14682,14526],"222":[13473,13518,13753,13561,13602,13640,13541,13612,13676,13575,13707,13686],"223":[12645,12704,12537,12636,12771,12536,12564,12643,12710,12794,12660,12535],"224":[11755,11700,11575,11657,11481,11448,11591,11547,11438,11648,11707,11473],"225":[10639,10612,10529,10628,10538,10554,10570,10561,10528,10572,10569,10666],"226":[9701,9535,9613,9715,9542,9593,9743,9505,9713,9764,9674,9730],"227":[8706,8857,8690,9020,8826,8816,8521,8811,8679,8842,8857,8776],"228":[8073,7913,8009,7909,7748,7998,7802,7974,7806,7983,8020,7775],"229":[6977,7022,7013,7223,7156,6970,7054,7002,7038,7030,7116,7191],"230":[6233,6342,6293,6370,6403,6305,6303,6330,6270,6310,6371,6264],"231":[5608,5662,5591,5648,5642,5708,5541,5655,5602,5646,5719,5565],"232":[5055,4900,4934,4996,5101,5015,5089,4874,4948,4887,4882,4895],"233":[4281,4414,4426,4413,4382,4348,4385,4379,4466,4417,4308,4405],"234":[3949,3832,3792,3794,3851,3815,3914,3842,3866,3924,3783,3794],"235":[3399,3315,3372,3336,3344,3449,3325,3309,3445,3359,3403,3445],"236":[2916,2949,3046,2987,2998,2916,2953,2910,3017,2881,2915,2885],"237":[2469,2553,2607,2591,2593,2554,2549,2530,2527,2536,2473,2517],"238":[2283,2125,2082,2147,2189,2246,2135,2208,2122,2224,2222,2123],"239":[1840,1834,1939,1932,1853,1821,1886,1860,1872,1819,1951,1889],"240":[1551,1592,1586,1560,1562,1589,1605,1600,1643,1590,1621,1700],"241":[1323,1374,1404,1315,1341,1283,1345,1345,1253,1387,1380,1366],"242":[1100,1149,1117,1100,1122,1131,1106,1137,1116,1094,1154,1137],"243":[937,921,924,996,949,950,969,1003,970,946,925,1070],"244":[836,841,804,774,810,815,759,753,779,786,784,803],"245":[643,734,660,694,662,682,681,678,659,675,658,677],"246":[571,532,584,517,577,571,558,592,591,548,551,517],"247":[443,419,458,406,459,476,471,488,458,435,461,464],"248":[394,356,396,393,404,395,383,367,367,387,342,393],"249":[315,300,343,323,285,280,316,290,348,304,286,336],"250":[252,260,250,248,245,251,270,252,257,239,239,256],"251":[184,219,191,198,189,203,202,210,204,196,211,188],"252":[164,155,159,167,178,154,172,159,160,154,153,149],"253":[129,126,152,150,129,111,135,117,103,122,149,143],"254":[103,124,105,126,129,90,117,86,77,119,109,114],"255":[84,76,82,98,70,81,91,85,84,66,66,61],"256":[85,69,69,73,69,73,64,58,74,69,74,71],"257":[56,51,60,65,72,49,46,54,52,51,52,63],"258":[42,50,43,46,47,48,46,42,50,40,42,24],"259":[35,38,39,40,28,29,35,24,26,24,29,35],"260":[25,27,25,31,22,29,30,29,40,24,29,25],"261":[14,17,17,25,19,23,20,23,24,19,13,23],"262":[9,16,15,13,21,18,16,10,12,21,19,16],"263":[10,11,13,10,9,21,11,7,10,7,6,10],"264":[4,7,7,11,10,6,5,9,10,11,9,9],"265":[4,8,5,5,8,4,11,4,3,14,8,9],"266":[6,5,4,3,8,9,7,6,5,6,3,2],"267":[7,6,7,3,2,2,3,1,1,5,4,4],"268":[4,2,1,2,0,3,2,3,2,4,6,2],"269":[0,1,2,5,4,1,2,3,1,2,3,4],"270":[1,0,0,3,2,1,2,7,6,1,3,2],"271":[1,0,0,0,2,0,1,0,0,2,1,2],"272":[1,1,0,0,2,1,2,1,1,0,0,0],"273":[0,3,1,1,0,0,1,0,1,0,1,1],"274":[0,1,2,1,1,0,0,0,0,0,0,0],"275":[2,0,0,0,1,0,0,0,0,0,0,0],"276":[0,0,2,0,0,0,0,1,0,0,0,0],"277":[0,0,0,0,1,0,0,0,1,0,0,0],"278":[0,2,0,0,0,0,0,0,0,0,0,0],"279":[0,0,0,0,0,0,0,2,0,0,0,0],"281":[1,0,0,0,0,1,0,0,0,0,0,0]}`
)

func SnappyBase64Encode(src []byte) []byte {
	got := snappy.Encode(nil, src)
	buf := make([]byte, base64.StdEncoding.EncodedLen(len(got)))
	base64.StdEncoding.Encode(buf, got)
	return buf
}

func SnappyBase64Decode(src []byte) (out []byte, err error) {
	var n int
	dBuf := make([]byte, base64.StdEncoding.DecodedLen(len(src)))
	n, err = base64.StdEncoding.Decode(dBuf, src)
	if err != nil {
		return
	}

	out, err = snappy.Decode(nil, dBuf[:n])
	if err != nil {
		return
	}

	return
}

func main() {
	enStr := SnappyBase64Encode([]byte(s))
	deStr, err := SnappyBase64Decode(enStr)
	if err != nil {
		fmt.Println("SnappyBase64Decode", err)
		return
	}

	fmt.Println(bytes.EqualFold([]byte(s), deStr))
	fmt.Println("s len", len(s), "enStr len", len(enStr))
}

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