fix: golanglint

This commit is contained in:
Flc゛ 2023-04-26 22:55:54 +08:00
parent 72838a5d2e
commit 5c59aae4cb
2 changed files with 17 additions and 9 deletions

View File

@ -456,7 +456,7 @@ func (c *Context) QueryArray(key string) (values []string) {
} }
func (c *Context) initQueryCache() { func (c *Context) initQueryCache() {
if c.queryCache == nil { if c.queryCache == nil { // nolint: errcheck
if c.Request != nil { if c.Request != nil {
c.queryCache = c.Request.URL.Query() c.queryCache = c.Request.URL.Query()
} else { } else {
@ -526,7 +526,7 @@ func (c *Context) PostFormArray(key string) (values []string) {
} }
func (c *Context) initFormCache() { func (c *Context) initFormCache() {
if c.formCache == nil { if c.formCache == nil { // nolint: errcheck
c.formCache = make(url.Values) c.formCache = make(url.Values)
req := c.Request req := c.Request
if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil { if err := req.ParseMultipartForm(c.engine.MaxMultipartMemory); err != nil {
@ -852,13 +852,19 @@ func bodyAllowedForStatus(status int) bool {
// Status sets the HTTP response code. // Status sets the HTTP response code.
func (c *Context) Status(code int) { func (c *Context) Status(code int) {
c.Writer.WriteHeader(code) if w, ok := c.Writer.(http.ResponseWriter); ok {
w.WriteHeader(code)
}
} }
// Header is an intelligent shortcut for c.Writer.Header().Set(key, value). // Header is an intelligent shortcut for c.Writer.Header().Set(key, value).
// It writes a header in the response. // It writes a header in the response.
// If value == "", this method removes the header `c.Writer.Header().Del(key)` // If value == "", this method removes the header `c.Writer.Header().Del(key)`
func (c *Context) Header(key, value string) { func (c *Context) Header(key, value string) {
if _, ok := c.Writer.(http.ResponseWriter); !ok {
return
}
if value == "" { if value == "" {
c.Writer.Header().Del(key) c.Writer.Header().Del(key)
return return
@ -1055,10 +1061,12 @@ func (c *Context) FileFromFS(filepath string, fs http.FileSystem) {
// FileAttachment writes the specified file into the body stream in an efficient way // FileAttachment writes the specified file into the body stream in an efficient way
// On the client side, the file will typically be downloaded with the given filename // On the client side, the file will typically be downloaded with the given filename
func (c *Context) FileAttachment(filepath, filename string) { func (c *Context) FileAttachment(filepath, filename string) {
if isASCII(filename) { if w, ok := c.Writer.(http.ResponseWriter); ok {
c.Writer.Header().Set("Content-Disposition", `attachment; filename="`+filename+`"`) if isASCII(filename) {
} else { w.Header().Set("Content-Disposition", `attachment; filename="`+filename+`"`) // nolint: errcheck
c.Writer.Header().Set("Content-Disposition", `attachment; filename*=UTF-8''`+url.QueryEscape(filename)) } else {
w.Header().Set("Content-Disposition", `attachment; filename*=UTF-8''`+url.QueryEscape(filename))
}
} }
http.ServeFile(c.Writer, c.Request, filepath) http.ServeFile(c.Writer, c.Request, filepath)
} }

4
gin.go
View File

@ -658,8 +658,8 @@ func serveError(c *Context, code int, defaultMessage []byte) {
return return
} }
if c.writermem.Status() == code { if c.writermem.Status() == code {
c.writermem.Header()["Content-Type"] = mimePlain c.writermem.Header()["Content-Type"] = mimePlain //nolint:errcheck
_, err := c.Writer.Write(defaultMessage) _, err := c.Writer.Write(defaultMessage) //nolint:errcheck
if err != nil { if err != nil {
debugPrint("cannot write message to writer during serve error: %v", err) debugPrint("cannot write message to writer during serve error: %v", err)
} }