我想爲某個REST API編碼一些JSON,除了一些錯誤,一切工作正常。例如,這個結構:當編碼爲JSON時,Golang錯誤類型爲空
type TemplateResponse struct {
Message string
Error error
Template Template
}
與此數據編碼:
res := TemplateResponse{"Template not found.", fmt.Errorf("There is no template on this host with the name " + vars["name"]), Template{}}
json.NewEncoder(w).Encode(res)
返回:
{
"Message": "Template not found.",
"Error": {},
"Template": {
"Name": "",
"Disabled": false,
"Path": "",
"Version": ""
}
}
我在我的應用程序獲得這個看似隨機,其中 '錯誤'類型被返回爲空。有任何想法嗎?
謝謝!
完美感。非常感謝這個偉大的解釋! – putitonmytab