亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試在 go 中創建可重用的附加結構

嘗試在 go 中創建可重用的附加結構

Go
慕后森 2023-07-31 16:14:12
我正在嘗試在 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"})。


查看完整回答
反對 回復 2023-07-31
  • 1 回答
  • 0 關注
  • 126 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號