纯讨论,逻辑判断全部goto而不是用common func(传入http上下文) 来解决
你怎么看?
var (
id = c.Param("id")
head = c.PostForm("head")
name = c.PostForm("name")
sex = c.PostForm("sex")
)
if id == "" {
c.JSON(200, gin.H{
"code": 4000,
"msg": "user id is not exist",
})
return
}
nameByte := []byte(name)
for _, ch := range nameByte {
switch {
case ch > '~':
fallthrough
case ch == '\r':
fallthrough
case ch == '\n':
fallthrough
case ch == '\t':
fallthrough
case ch < ' ':
goto nameError
}
}
name = strings.Replace(name, " ", "", -1)
if len(name) == 0 {
goto nameError
} else {
goto nameContinue
}
nameError:
c.JSON(200, gin.H{
"code": 4001,
"msg": "昵称不合法",
})
return
nameContinue:
if len(sex) != 1 {
c.JSON(200, gin.H{
"code": 4002,
"msg": "性别格式不合法",
})
}
不建议在这种场景下使用goto