问题描述
我正在编写一个GO应用程序,它使用编码/ gob在节点之间通过UDP发送结构和切片。它工作正常,但我注意到,编码/ JSON也有类似的API。搜索并找到这些信息(): p>
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/):
有人可以解释对我来说,一个人是否比另一个更有效率,并且总体上比较何时选择什么?此外,如果我需要与非golang应用程序接口,我想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-golang application, I guess json would be preferred?
推荐答案
Gob更受欢迎在Go程序之间进行通信时。但是,目前仅在Go中支持gob,并且只支持gob当你确定没有任何其他编程语言编写的程序会尝试解码这些值。
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
这篇关于golang中encoding / gob和encoding / json之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!