Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does gob encoding do?

Tags:

encoding

go

gob

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
like image 458
irom Avatar asked Dec 19 '25 22:12

irom


1 Answers

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

like image 83
Shmulik Klein Avatar answered Dec 23 '25 17:12

Shmulik Klein



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!