我正在嘗試在 go 中創建可重用的方法/函數,以將值結構推送到結構中的另一個切片/數組我試過這樣import ( "fmt")type ErrorValidate struct { ErrorKey string Message string }type ValidateMessage struct { ErrorMessage []*ErrorValidate}func (v *ValidateMessage) AddError(err ErrorValidate) { v.ErrorMessage = append(v.ErrorMessage, &err)}func main() { s1 := *ValidateMessage{} s1.AddError(&ErrorValidate{"theKey", "string"}) fmt.Println(*s1)}出現錯誤invalid indirect of ValidateMessage literal (type ValidateMessage)鏈接在這里https://play.golang.org/p/VjdsiZQLroF在這種情況下,我有一個用于驗證某些內容的功能,然后我嘗試在 ErrorValidate 上推送錯誤消息,但我繼續在條件中使用追加,我試圖減少它,但出現了上面的錯誤
1 回答

瀟湘沐
TA貢獻1816條經驗 獲得超6個贊
您的代碼中有幾個問題。這個正在生成您的錯誤(與您的附加無關,或根本與該方法無關):
s1 := *ValidateMessage{}
這不是有效的語法。你可能是說s1 := &ValidateMessage{}
。
s1.AddError(&ErrorValidate{"theKey", "string"})
您正在嘗試將 a 傳遞*ErrorValidate
給需要 a 的函數ErrorValidate
。這應該是s1.AddError(ErrorValidate{"theKey", "string"})
。
- 1 回答
- 0 關注
- 126 瀏覽
添加回答
舉報
0/150
提交
取消