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/):
我正在GO中编写一个应用程序,它使用encoding / gob在节点之间通过UDP发送结构和切片。它工作正常,但我注意到encoding / json也有类似的API。搜索并找到此信息(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.gob Package gob管理gobs流 - 在编码器(发送器)和解码器(接收器)之间交换的二进制值。 json包json实现了RFC 4627中定义的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?
有人可以向我解释一个人是否比另一个人更有效率,并且一般比较什么时候选择什么?另外如果我需要与非golang应用程序接口,我猜json会更受欢迎吗?
2 个解决方案
#1
6
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.
在Go程序之间进行通信时,Gob更受欢迎。但是,gob目前仅支持Go,以及C,所以只有当你确定没有用任何其他编程语言编写的程序试图解码这些值时才使用它。
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)
说到性能,至少在我的机器上,Gob远远超过了JSON。测试文件(放在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
#2
5
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.
包编码/ gob基本上是Go特定的,并且不能与其他语言一起使用,但它非常有效(快速并生成小数据)并且可以正确地编组和解组更多数据结构。通过JSON,通常可以更轻松地与其他工具连接。
#1
6
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.
在Go程序之间进行通信时,Gob更受欢迎。但是,gob目前仅支持Go,以及C,所以只有当你确定没有用任何其他编程语言编写的程序试图解码这些值时才使用它。
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)
说到性能,至少在我的机器上,Gob远远超过了JSON。测试文件(放在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
#2
5
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.
包编码/ gob基本上是Go特定的,并且不能与其他语言一起使用,但它非常有效(快速并生成小数据)并且可以正确地编组和解组更多数据结构。通过JSON,通常可以更轻松地与其他工具连接。