package gupta import ( "encoding/json" "fmt" "time" ) const version = "0.0.1" func Hello() string { return fmt.Sprintf("Welcome to gupta version %s", version) } type GuptaReport struct { Time time.Time LoadAvarage float64 CPUUsage *CPUStat `json:"cpu usage, omitempty"` MemoryUsage *Memory `json:"memory,omitempty"` } // usually you'd want a g GuptaReport reciever here ... // but than you could now check for the object properties ... func (g *GuptaReport) MarshalJSON() ([]byte, error) { report := []byte(fmt.Sprintf("{\"timestamp\": \"%d\", \"metrics\": [", g.Time.Unix())) if g.CPUUsage != nil { usage, _ := json.Marshal(g.CPUUsage) report = append(report, usage...) } if g.MemoryUsage != nil { if g.CPUUsage != nil { report = append(report, []byte(",")...) } usage, _ := json.Marshal(g.MemoryUsage) report = append(report, usage...) } report = append(report, []byte("]}")...) return report, nil }