簡體   English   中英

在 golang 中實現 Scan 和 Value 函數

[英]Implement Scan and Value functions in golang

我正在嘗試在 SQL 數據庫中存儲一些 golang 對象,並實現了掃描儀和值接口,如下所示:

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:hugeParam
func (attr mystruct) Value() (driver.Value, error) {
    return json.Marshal(attr)
}

有沒有一種方法可以通過指針將參數傳遞給Value() () 函數,因為在嘗試將數據轉換回我的結構時,我收到一個HugeParam錯誤,即傳遞給Value()函數的 attr 太大.

任何建議表示贊賞,謝謝!

更新:我試圖通過標記 nolint 來解決這個問題,以忽略巨大的參數,但錯誤仍然存​​在。 這是錯誤消息:

golangci-lint run --deadline 300s

feedback-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)

使固定:

/nolint // hugeParam: //insert explanation
func (attr mystruct) Value() (driver.Value, error) {
    return json.Marshal(attr)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM