package user // UserDto 用户数据传输对象 type UserDto struct { Uid uint64 `json:"uid"` Username string `json:"username" binding:"required"` Phone string `json:"phone" binding:"required"` Password bool `json:"password,omitempty"` GiftCount int8 `json:"giftCount"` Status int8 `json:"status"` Remark string `json:"remark"` } type userDtoConfiguration func(dto *UserDto) type userDtoConfigurations []userDtoConfiguration // WithDtoUsername 设置用户名 func WithDtoUsername(username string) userDtoConfiguration { return func(dto *UserDto) { dto.Username = username } } // WithDtoPhone 设置手机号 func WithDtoPhone(phone string) userDtoConfiguration { return func(dto *UserDto) { dto.Phone = phone } } func NewUserDto(cfg ...userDtoConfiguration) *UserDto { userDto := &UserDto{} userDtoConfigurations(cfg).apply(userDto) return userDto } func (cfg userDtoConfigurations) apply(userDto *UserDto) { for _, c := range cfg { c(userDto) } }