douka19950505 2016-12-16 07:39
浏览 500
已采纳

Golang中encoding / gob和encoding / json之间的区别

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 similar API. Searched and found this information(https://golang.org/pkg/encoding/):

gob Package gob manages streams of gobs - binary values exchanged between an Encoder (transmitter) and a Decoder (receiver).
json Package json implements encoding and decoding of JSON as defined in RFC 4627.

Can someone explain to me whether one is more efficient than the other and in general compare when to choose what? Also if I need to interface with a non-golang application, I guess json would be preferred?

  • 写回答

2条回答 默认 最新

  • dsjxgu4759 2016-12-16 09:04
    关注

    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
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码