Search code examples
goencodinggob

What does gob encoding do?


Does gob encoding/decoding do anything ? In the example below , data looks the same before and after decoding. I am confused, please advise

data = "ABC"
    buf := new(bytes.Buffer)

    //glob encoding
    enc := gob.NewEncoder(buf)
    enc.Encode(data)
    fmt.Println("Encoded:", data)  //Encoded: ABC

    //glob decoding
    d := gob.NewDecoder(buf)
    d.Decode(data)
    fmt.Println("Decoded: ", data) //Decoded:  ABC

Solution

  • Your comparison is wrong - comparing the data being encoded (data) to the result after being decoded (d.Decode(data)), will obviously lead you to the same result (if everything is working as expected).

    The encoding itself will be presented in the underline bytes buffer (try to print the buffer itself - fmt.Println(buf.Bytes())).

    Read more on the gob package