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

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

在 golang 中實現 Scan 和 Value 函數

在 golang 中實現 Scan 和 Value 函數

Go
ibeautiful 2022-12-19 19:26:05
我正在嘗試將一些 golang 對象存儲在 SQL 數據庫中,并按如下方式實現了掃描器和值接口:func (attr *myStruct) Scan(src interface{}) error {    switch v := src.(type) {    case string:        return json.Unmarshal([]byte(v), attr)    case []byte:        return json.Unmarshal(v, attr)    }    return fmt.Errorf("cannot convert %T to My struct", src)}//nolint:hugeParamfunc (attr mystruct) Value() (driver.Value, error) {    return json.Marshal(attr)}有沒有一種方法可以通過Value()指針將參數傳遞給函數,因為在嘗試將數據轉換回我的結構時出現HugeParam錯誤,即傳遞給Value()函數的 attr 太大。任何建議表示贊賞,謝謝!更新:我試圖通過標記一個 nolint 來忽略巨大的參數來解決這個問題,但錯誤仍然存在。這是錯誤消息:golangci-lint run --deadline 300sfeedback-handler-test_1_fdc66eade9d0 | level=warning msg="[runner] The linter 'interfacer' is deprecated (since v1.38.0) due to: The repository of the linter has been archived by the owner. "feedback-handler-test_1_fdc66eade9d0 | level=warning msg="[runner/nolint] Found unknown linters in //nolint directives: gocritic:hugeparam"feedback-handler-test_1_fdc66eade9d0 | internal/app/domain/entity/feedbacks.go:61:7: hugeParam: attr is heavy (320 bytes); consider passing it by pointer (gocritic)
查看完整描述

1 回答

?
天涯盡頭無女友

TA貢獻1831條經驗 獲得超9個贊

使固定:


/nolint // hugeParam: //insert explanation

func (attr mystruct) Value() (driver.Value, error) {

    return json.Marshal(attr)

}


查看完整回答
反對 回復 2022-12-19
  • 1 回答
  • 0 關注
  • 409 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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