From 85f3e78abcd408e7e155f58f72094446c4411e91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=94=B0=E6=AC=A7?= Date: Mon, 20 Aug 2018 21:49:24 +0800 Subject: [PATCH] chore: remove else instead of return/continue (#1502) As[ Effective Go](https://golang.org/doc/effective_go.html?#if) about `if` said, remove else statement instead of return/continue statement. --- binding/form_mapping.go | 16 ++++++++-------- context.go | 8 ++++---- render/json.go | 6 ++---- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/binding/form_mapping.go b/binding/form_mapping.go index a714291..f46a0dc 100644 --- a/binding/form_mapping.go +++ b/binding/form_mapping.go @@ -74,16 +74,16 @@ func mapForm(ptr interface{}, form map[string][]string) error { } } val.Field(i).Set(slice) - } else { - if _, isTime := structField.Interface().(time.Time); isTime { - if err := setTimeField(inputValue[0], typeField, structField); err != nil { - return err - } - continue - } - if err := setWithProperType(typeField.Type.Kind(), inputValue[0], structField); err != nil { + continue + } + if _, isTime := structField.Interface().(time.Time); isTime { + if err := setTimeField(inputValue[0], typeField, structField); err != nil { return err } + continue + } + if err := setWithProperType(typeField.Type.Kind(), inputValue[0], structField); err != nil { + return err } } return nil diff --git a/context.go b/context.go index 6d80284..063c72f 100644 --- a/context.go +++ b/context.go @@ -665,9 +665,9 @@ func (c *Context) Status(code int) { func (c *Context) Header(key, value string) { if value == "" { c.Writer.Header().Del(key) - } else { - c.Writer.Header().Set(key, value) + return } + c.Writer.Header().Set(key, value) } // GetHeader returns value from request headers. @@ -755,9 +755,9 @@ func (c *Context) JSONP(code int, obj interface{}) { callback := c.DefaultQuery("callback", "") if callback == "" { c.Render(code, render.JSON{Data: obj}) - } else { - c.Render(code, render.JsonpJSON{Callback: callback, Data: obj}) + return } + c.Render(code, render.JsonpJSON{Callback: callback, Data: obj}) } // JSON serializes the given struct as JSON into the response body. diff --git a/render/json.go b/render/json.go index 6e5089a..4d1857f 100644 --- a/render/json.go +++ b/render/json.go @@ -128,10 +128,8 @@ func (r AsciiJSON) Render(w http.ResponseWriter) (err error) { var buffer bytes.Buffer for _, r := range string(ret) { - cvt := "" - if r < 128 { - cvt = string(r) - } else { + cvt := string(r) + if r >= 128 { cvt = fmt.Sprintf("\\u%04x", int64(r)) } buffer.WriteString(cvt)