I'm trying to make the Error
and Success
struct disappear if either one of them is empty
package main
import (
"encoding/json"
"net/http"
)
type appReturn struct {
Suc *Success `json:"success,omitempty"`
Err *Error `json:"error,omitempty"`
}
type Error struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
type Success struct {
Code int `json:"code,omitempty"`
Message string `json:"message,omitempty"`
}
func init() {
http.HandleFunc("/", handler)
}
func handler(w http.ResponseWriter, r *http.Request) {
j := appReturn{&Success{}, &Error{}}
js, _ := json.Marshal(&j)
w.Header().Set("Content-Type", "application/json")
w.Write(js)
}
Output:
{
success: { },
error: { }
}
How can I hide the Error
or Success
struct from the JSON output?
I thought that sending the pointer as an argument would do the trick.