-
-
Save WonderfulSoap/18a14da135f659d5350f36bdbe439b6a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
// 某个实际项目复杂业务的DTO定义,字段数量规模相似,(仅做演示,并非所有字段都为int类型) | |
// 大部分字段都不可为null,只有少数几个字的可null | |
type Data2 struct { | |
A *int | |
B *int | |
C *int | |
D *int | |
E *int | |
F *int | |
G *int | |
H *int | |
I *int //可null字段 | |
J *int //可null字段 | |
K *int | |
L *int | |
M *int | |
N *int | |
O *int // 可null字段 | |
P *int | |
Q *int // 可null字段 | |
R *int | |
S *int | |
T *int | |
U *int | |
V *int | |
W *int | |
X *int | |
Y *int | |
Z *int | |
Obj1 *struct { | |
A *int | |
B *int | |
C *int | |
D *int | |
E *int | |
F *int | |
G *int | |
H *int | |
I *int //可null字段 | |
J *int //可null字段 | |
K *int | |
L *int | |
M *int | |
N *int | |
O *int // 可null字段 | |
P *int | |
Q *int // 可null字段 | |
R *int | |
S *int | |
T *int | |
} | |
} | |
// validate 判断所有不可为null的字段是否为null。为null返回错误。标记为可null字段的无需判断 | |
func (d Data2) Validate() error { | |
if d.A == nil || d.B == nil || d.C == nil || d.D == nil || d.E == nil || d.F == nil || d.G == nil || d.H == nil|| ...... { | |
return fmt.Errorf("validation error") | |
} | |
return nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
func Calculate(name string, data Data2) int { | |
// 这里已经有两个bug了, data.Obj1.I 和 data.I 为可null字段,使用前必须判断nil | |
return (*data.A) * (*data.B) * (*data.C) * (*data.D) * (*data.I) * (*data.Obj1.A) * (*data.Obj1.B) * (*data.Obj1.I) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is what you want:
Error handling and wrapping are omitted for simplicity.