返回
error。使用可分辨的值(例如
nil)来指示错误不是惯用的。
func NewSomething(name, color string) (*Something, error) { if name == "" { return nil, errors.New("bad name") } if color == "" { return nil, errors.New("bad color") } s := Something{name, color} return &s, nil}另外:表达式的
&anyVariable == nil计算结果始终为
false。简化对
len(color) == 0或的检查
color ==""。



