编码/目标和编码/json之间的区别 [英] difference between encoding/gob and encoding/json

查看:118
本文介绍了编码/目标和编码/json之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Go中编写一个应用程序,该应用程序使用编码/目标在节点之间通过UDP发送结构和切片.它工作正常,但我注意到encoding/json也具有类似的API.搜索并找到此信息( https://golang.org/pkg/encoding/):

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软件包gob管理gobs流-交换二进制值 在编码器(发送器)和解码器(接收器)之间.
json包json实现JSON中的编码和解码,如 RFC 4627.

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.

有人可以向我解释一种方法是否比另一种方法更有效,并且通常比较何时选择哪种方法?另外,如果我需要与非Go应用程序接口,我猜json将是首选?

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-Go application, I guess json would be preferred?

推荐答案

Gob更为可取.但是,go仅在Go中受支持,而且 C 当您确定没有其他任何编程语言编写的程序会尝试对这些值进行解码时,请使用该值.

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.

在性能方面,至少在我的机器上,Gob远胜过JSON. 测试文件(在您的GOPATH下单独放入一个文件夹中)

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

这篇关于编码/目标和编码/json之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆