记一次Golang代码重构

golang 17-07-12 16:30 2001  

### 重构之前: ``` func Upload(c *gin.Context) { file, _, err := c.Request.FormFile("file") if err != nil { c.JSON(http.StatusOK, gin.H{ "message": err.Error(), }) } else { //filename := header.Filename //fmt.Println(filename) key, err := uploadFile(file) if err != nil { c.JSON(http.StatusOK, gin.H{ "message": err.Error(), }) } else { c.JSON(http.StatusOK, gin.H{ "url": system.GetConfiguration().QiniuFileServer + key, }) } } } ``` ### 重构之后: ``` func Upload(c *gin.Context) { var err error file, _, err := c.Request.FormFile("file") if err == nil { var key string key, err = uploadFile(file) if err == nil { c.JSON(http.StatusOK, gin.H{ "url": system.GetConfiguration().QiniuFileServer + key, }) return } } c.JSON(http.StatusOK, gin.H{ "message": err.Error(), }) } ```