by voidlogic · 11y ago · 0 comments
Or even better perf/safety wise:
type Payload struct {
Name string `json:"name"`
Category string `json:"category"`
Action string `json:"action"`
Where string `json:"where"`
Timestamp int64 `json:"timestamp,string"`
}
payload := Payload{
Name: "joe smith",
Category: "people",
Action: "transport"
Where: "pluto",
Timestamp: time.Now().Unix(),
}
jsonBytes, err := json.Marshal(&payload)
This code doesn't allocate heap for payload, is much faster and typesafe...P.S. And could be shorter, but I was trying to be clear.
No comments yet.