difference between encoding/gob and encoding/json

前端 未结 2 1838
南方客
南方客 2021-02-07 13:31

I am writing an application in Go which uses encoding/gob to send structures and slices over UDP between nodes. It works fine but I notice that encoding/json also has the simila

相关标签:
2条回答
  • 2021-02-07 13:53

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

    When it comes to performance, at least on my machine, Gob outperforms JSON by a long shot. Test file (put in a folder on its own 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
    
    0 讨论(0)
  • 2021-02-07 13:53

    Package encoding/gob is basically Go specific and unusable with other languages but it is very efficient (fast and generates small data) and can properly marshal and unmarshal more data structures. Interfacing with other tools is often easier via JSON.

    0 讨论(0)
提交回复
热议问题