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条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题