考虑以下代码,它响应 GET '/venues/:id':
func venueShow(w http.ResponseWriter, req *http.Request) {
// get ID from params
vars := mux.Vars(req)
id := vars["id"]
// initialise new struct
var venue Venue
// select by id and scan into struct
db.First(&venue, id).Scan(&venue)
// turn it to json
response := structToJSON(&venue)
// write headers and provide response
w.Header().Set("Content-Type", "application/json")
w.Write(response)
}
Run Code Online (Sandbox Code Playgroud)
和:
func structToJSON (s interface{}) (response []byte) {
// turn it into pretty-ish json
response, err := json.MarshalIndent(&s, "", " ")
if err != nil {
return []byte("Venue does not exist")
}
// return the json as the reponse
return response
}
Run Code Online (Sandbox Code Playgroud)
我的 structToJSON 函数将一个空接口作为参数,因为我想将各种不同的结构传递给该函数并将它们作为 JSON 输出。
然而,它并没有让我觉得很安全。如果有任何东西满足一个空接口,我可以将任何我想要的东西传递给那个函数,当 json.Marshal 尝试做它的事情时,可能会发生各种错误。这(我想)会被编译器捕获而不是在运行时捕获,但是有没有更安全的方法?
我可以为传递给它的每种不同类型的结构/模型复制 structToJSON 方法,但这不是很 DRY。
谢谢
Marshal 函数也接收其参数,interface{}
因此无法检测您是否在编译时传递了无效的内容,所有这些都在运行时被捕获。
要检查是否将无效类型传递给 Marshal,您可以做的一件事是检查错误类型,UnsupportedTypeError
当您尝试编组无效类型(如chan
或func
)时,Marshal 会返回 ,以便您可以在编组时检查该错误。
所以你可以尝试这样的事情:
if err != nil {
_, ok := err.(*json.UnsupportedTypeError)
if ok {
return []byte("Tried to Marshal Invalid Type")
} else {
return []byte("Venue does not exist")
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
10660 次 |
最近记录: |