Skip to content

Instantly share code, notes, and snippets.

@miguelmota
Last active November 3, 2020 10:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save miguelmota/d7a66eaea680b460cb7799cc16916364 to your computer and use it in GitHub Desktop.
Save miguelmota/d7a66eaea680b460cb7799cc16916364 to your computer and use it in GitHub Desktop.
Golang snappy encode and decode example
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
}
@edwardIshaq
Copy link

I'm not yet clear on the purpose of passing in dst? it seems like some sort of auxilary slice of some sort

@chrispassas
Copy link

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.

@miguelmota
Copy link
Author

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