我正在尝试将 ddd 概念应用于我的 go 项目然后我有一个实体,其中包含如下嵌套结构:type Person struct { Id string Name valueObject.Name Email valueObject.Email Password valueObject.Password Created time.Time Updated time.Time} func NewPerson(name valueObject.Name, email valueObject.Email, password valueObject.Password) *Person { return &Person{ Id: "a", Name: name, Email: email, Password: password, Created: time.Now(), Updated: time.Now(), } }我的所有对象值都有一个类似于下面的结构:type Email struct { address string}func NewEmail(address string) (Email, error) { isValid := valid(address) if !isValid { return Email{}, ErrEmail } return Email{address}, nil}func (e Email) Value() string { return e.address}func (e Email) Format() string { return strings.TrimSpace(strings.ToLower(e.Value()))}func (e Email) Equals(e2 Email) bool { return e.Value() == e2.Value()}然后,由于我有一个嵌套的结构,我想创建一个ToJSON函数,以返回我的数据,并且我还需要排除一些返回作为密码:type personJson struct { id string name string email string createdAt time.Time updatedAt time.Time}func (p *Person) ToJSON() personJson { email := p.Email.Value() name := p.Name.FullName() person := personJson{id: "dasdas", email: email, name: name, createdAt: p.Created, updatedAt: p.Updated} return person}但是在我的控制器中,我尝试访问它,或者当我返回此ToJSON时,始终显示为{}:type createPersonDTO struct { FirstName string `json:"firstName"` LastName string `json:"lastName"` Password string `json:"password"` Email string `json:"email"`}
1 回答
慕工程0101907
TA贡献1887条经验 获得超5个赞
要导出到的结构的每个字段的第一个字母大写:json.Marshal
type personJson struct { ID string Name string Email string CreatedAt time.Time UpdatedAt time.Time }
- 1 回答
- 0 关注
- 115 浏览
添加回答
举报
0/150
提交
取消