我正在嘗試將 ddd 概念應用于我的 go 項目然后我有一個實體,其中包含如下嵌套結(jié)構(gòu):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(), } }我的所有對象值都有一個類似于下面的結(jié)構(gòu):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()}然后,由于我有一個嵌套的結(jié)構(gòu),我想創(chuàng)建一個ToJSON函數(shù),以返回我的數(shù)據(jù),并且我還需要排除一些返回作為密碼: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條經(jīng)驗 獲得超5個贊
要導出到的結(jié)構(gòu)的每個字段的第一個字母大寫:json.Marshal
type personJson struct { ID string Name string Email string CreatedAt time.Time UpdatedAt time.Time }
- 1 回答
- 0 關(guān)注
- 135 瀏覽
添加回答
舉報
0/150
提交
取消