Difference between / gob encoding and / json encoding in golang

I am writing an application in GO that uses the / gob encoding to send structures and slices over UDP between nodes. It works fine, but I notice that the / json encoding also has a similar API. I searched and found this information ( https://golang.org/pkg/encoding/ ):

gob The gob package controls gobs streams - binary values ​​are exchanged between the sensor (transmitter) and the decoder (receiver).
json Package json implements JSON encoding and decoding as defined in RFC 4627.

Can someone explain to me whether someone is more effective than the other, and generally compare when to choose what? Also, if I need to interact with a non-golang application, I think json would be preferable?

+4
source share
2 answers

Gob is much more preferable when communicating between Go programs. However, gob is currently only supported in Go and, well, C , so always use this when you are sure that no program is written in any other programming language that will try to decode the values.

When it comes to performance, at least on my machine, Gob outperformed JSON with a long shot. test file (put it in a folder under your GOPATH)

$ go test -bench=.        
testing: warning: no tests to run
BenchmarkGobEncoding-4           1000000              1172 ns/op
BenchmarkJSONEncoding-4           500000              2322 ns/op
BenchmarkGobDecoding-4           5000000               486 ns/op
BenchmarkJSONDecoding-4           500000              3228 ns/op
PASS
ok      testencoding    6.814s
+4
source

/gob Go , ( ) . JSON.

+4

Source: https://habr.com/ru/post/1663922/


All Articles