Bad Go: Adventures with JSON marshalling

type mystruct struct {
A int `json:"a,omitempty"`
B string `json:"b,omitempty"`
}
func BenchmarkJSONMarshal(b *testing.B) {
b.ReportAllocs()
var data = mystruct{
A: 42,
B: "42",
}
for i := 0; i < b.N; i++ {
_, err := json.Marshal(&data)
if err != nil {
b.Errorf("failed to marshal json. %s", err)
}
}
}
BenchmarkJSONMarshal-8 3627376 316 ns/op 32 B/op 1 allocs/op
type mystruct struct {
A int `json:"a,omitempty"`
B string `json:"b,omitempty"`
C time.Time `json:"c"`
}
func BenchmarkJSONMarshal(b *testing.B) {
b.ReportAllocs()
var data = mystruct{
A: 42,
B: "42",
C: time.Now(),
}
for i := 0; i < b.N; i++ {
_, err := json.Marshal(&data)
if err != nil {
b.Errorf("failed to marshal json. %s", err)
}
}
}
BenchmarkJSONMarshal-8 981222 1345 ns/op 208 B/op 4 allocs/op
func BenchmarkTimeMarshal(b *testing.B) {
b.ReportAllocs()
var t time.Time
for i := 0; i < b.N; i++ {
_, err := t.MarshalJSON()
if err != nil {
b.Errorf("failed to marshal. %s", err)
}
}
}
BenchmarkTimeMarshal-8 3400222 378 ns/op 48 B/op 1 allocs/op
type MarshalAppender interface {
MarshalAppendJSON(buf []byte) ([]byte, error)
}

Is it Bad Go?

Next steps

--

--

--

software, fraud, bicycles — Fighting bad people at ravelin.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Installing Flutter on a Mac, and running your first Flutter app

Running Logentries in a Kubernetes Environment

Fixing Nuts auto-updates for our Electron app.

Agile Software Development: Way to Revolutionize the Software Industry

Download macOS Big Sur with gibMacOS for Clean Installation | Kextcache

What Would You Learn In a Year Of Free Fridays?

Why is the ‘demo’ adaptor missing from the list of

5 COMMON ISSUES THAT DECREASE SALESFORCE PERFORMANCE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Phil Pearl

Phil Pearl

software, fraud, bicycles — Fighting bad people at ravelin.com

More from Medium

gorilla/mux 101 (rk-boot): Add prometheus middleware

Getting started with GO Programming Language — Part Two

Concurrency in Golang

Golang Rest API Routing (using gorilla/mux)

Golang MUX Router Gopher Rest API