本文整理匯總了Golang中vulcan/kubernetes/pkg/runtime.Codec.EncodeToStream方法的典型用法代碼示例。如果您正苦於以下問題:Golang Codec.EncodeToStream方法的具體用法?Golang Codec.EncodeToStream怎麽用?Golang Codec.EncodeToStream使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類vulcan/kubernetes/pkg/runtime.Codec
的用法示例。
在下文中一共展示了Codec.EncodeToStream方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: writeJSON
// writeJSON renders an object as JSON to the response.
func writeJSON(statusCode int, codec runtime.Codec, object runtime.Object, w http.ResponseWriter, pretty bool) {
w.Header().Set("Content-Type", "application/json")
// We send the status code before we encode the object, so if we error, the status code stays but there will
// still be an error object. This seems ok, the alternative is to validate the object before
// encoding, but this really should never happen, so it's wasted compute for every API request.
w.WriteHeader(statusCode)
if pretty {
prettyJSON(codec, object, w)
return
}
err := codec.EncodeToStream(object, w)
if err != nil {
errorJSONFatal(err, codec, w)
}
}