Here’s how to compress and decompress a string in Golang using the gzip library.

package main

import (
    "compress/gzip"
    "fmt"
    "bytes"
    "io/ioutil"
)

// Credit where credit is due!
// https://stackoverflow.com/a/19267224
func compressString(str string) (string, error) {
    var b bytes.Buffer
    gz := gzip.NewWriter(&b)
    if _, err := gz.Write([]byte(str)); err != nil {
        fmt.Println(fmt.Sprintf("Compress string failure %v", err))
        return "", err
    }
    if err := gz.Close(); err != nil {
        fmt.Println(fmt.Sprintf("Compress string closure failure %v", err))
        return "", err
    }
    return string(b.Bytes()), nil
}

func decompressString(str string) (string, error) {
    gr, err := gzip.NewReader(bytes.NewBuffer([]byte(str)))
    defer gr.Close()
    data, err := ioutil.ReadAll(gr)
    if err != nil {
        return "", err
    }
    return string(data), nil
}

Go Playground Link