Last active
November 3, 2020 10:05
-
-
Save miguelmota/d7a66eaea680b460cb7799cc16916364 to your computer and use it in GitHub Desktop.
Golang snappy encode and decode example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"github.com/golang/snappy" | |
) | |
func main() { | |
src := []byte("ABCCCCCCCCCCCCCCCCCCC") | |
encoded := snappy.Encode(nil, src) | |
fmt.Println(string(encoded)) // ABCF | |
decoded, err := snappy.Decode(nil, encoded) | |
if err != nil { | |
log.Fatal(err) | |
} | |
fmt.Println(string(decoded)) // ABCCCCCCCCCCCCCCCCCCC | |
} |
It is confusing, if you pass dst the Decode() will use that existing slice of bytes to decompress into. If you don't use dst and just use the return "decoded" then Decode() creates a new slice of bytes for you.
This way if you have to decompress many things over and over you can reuse the same dst byte slice.
Normally you should return _, err = Decoder() or when calling Decode() pass nil for the dst field.
Thanks for the clarification. Example is updated
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm not yet clear on the purpose of passing in dst? it seems like some sort of auxilary slice of some sort