mirror of
https://github.com/chai2010/advanced-go-programming-book.git
synced 2025-05-23 20:02:22 +00:00
update validator
This commit is contained in:
parent
72206694c8
commit
eed914b514
@ -2,9 +2,38 @@
|
||||
|
||||
社区里曾经有人用这张图来嘲笑 PHP:
|
||||
|
||||

|
||||

|
||||
|
||||
实际上这是一个语言无关的场景,需要进行字段校验的情况有很多,web 系统的 Form/json 提交只是一个典型的例子。我们用 go 来写一个类似上图的校验 demo。然后研究怎么一步步对其进行改进。假设我们的数据已经通过某个 binding 库绑定到了具体的 struct 上。
|
||||
|
||||
```go
|
||||
```
|
||||
type RegisterReq struct {
|
||||
Username string
|
||||
PasswordNew string
|
||||
PasswordRepeat string
|
||||
Email string
|
||||
}
|
||||
|
||||
func register(req RegisterReq) error{
|
||||
if len(req.Username) > 0 {
|
||||
if len(req.PasswordNew) > 0 && len(req.PasswordRepeat) > 0 {
|
||||
if req.PasswordNew == req.PasswordRepeat {
|
||||
if emailFormatValid(req.Email) {
|
||||
createUser()
|
||||
return nil
|
||||
} else {
|
||||
return errors.New("invalid email")
|
||||
}
|
||||
} else {
|
||||
return errors.New("password and reinput must be the same")
|
||||
}
|
||||
} else {
|
||||
return errors.New("password and password reinput must be longer than 0")
|
||||
}
|
||||
} else {
|
||||
return errors.New("length of username cannot be 0")
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
我们在 golang 里成功写出了 hadoken 开路的箭头型代码。。这种代码一般怎么进行优化呢?
|
||||
|
Before Width: | Height: | Size: 59 KiB After Width: | Height: | Size: 59 KiB |
Loading…
x
Reference in New Issue
Block a user