在不会报错的函数中,通过返回原对象,可以定义链式操作,使代码简洁
type Student struct {
name string
age int
}
func (s *Student)SetName(name string) (*Student) {
s.name = name
return s
}
func (s *Student)SetAge(age int) (*Student) {
s.age = age
return s
}
func main() {
s := Student{}
s.SetName("li").SetAge(18)
fmt.Println(s)
}