Skip to content
Advertisement

Implement Scan and Value functions in golang

I am trying to store some golang objects in SQL database and have implemented the scanner and value interface as follows:

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

Is there a way that I can pass the parameter to the Value() function by pointers, as I am getting a HugeParam error that the attr passed to the Value() function is too big, when trying to convert the data back to my struct.

Any advise is appreciated, thanks!

Update: I am trying to solve this issue by marking a nolint to ignore the huge param but the error still persists. This is the error msg:

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)

Advertisement

Answer

Fix:

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

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement