我在使用時(shí)遇到了一些麻煩UnmarshalJSON,MarshalJSON我不確定我是否正確理解了它們,或者至少我有一個(gè)我無法發(fā)現(xiàn)的錯(cuò)誤,并且在我 3 小時(shí)的谷歌搜索中我還沒有找到,所以就這樣吧。我有以下用戶結(jié)構(gòu)type User struct { ID uuid.UUID `gorm:"primaryKey,type:string,size:36,<-:create" json:"id"` Username string `gorm:"unique" json:"username"` Password PasswordHash `gorm:"type:string" json:"password"` CreatedAt time.Time `gorm:"autoCreateTime:milli" json:"created_at"` UpdatedAt time.Time `gorm:"autoUpdateTime:milli" json:"updated_at,omitempty"` DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at,omitempty"`}當(dāng)我嘗試json.Marshal使用內(nèi)置encoding/json庫(kù)中的函數(shù)將其轉(zhuǎn)換為 JSON 時(shí),我得到以下信息:{"id":"3843298e-74d4-4dd7-8eff-007ab34a4c19","username":"root","password":{},"created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":null}我期待這樣的事情:{"id":"3843298e-74d4-4dd7-8eff-007ab34a4c19","username":"root","password":"$argon2id$v=19$m=4194304,t=1,p=64$Z9EFSTk26TQxx+Qv9g58gQ$4At0rvvv9trRcFZmSMXY0nISBuEt+1X8mCRAYbyXqSs","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","deleted_at":null}
1 回答

慕碼人8056858
TA貢獻(xiàn)1803條經(jīng)驗(yàn) 獲得超6個(gè)贊
您已經(jīng)MarshalJSON
定義了一個(gè)接收者*PasswordHash
,但您的值是類型PasswordHash
。將接收器更改為PasswordHash
并按預(yù)期工作:https ://go.dev/play/p/WukE_5JBEPL
- 1 回答
- 0 關(guān)注
- 105 瀏覽
添加回答
舉報(bào)
0/150
提交
取消