在Go语言中实现Scan和Value函数。

huangapple go评论82阅读模式
英文:

Implement Scan and Value functions in golang

问题

我正在尝试将一些 Golang 对象存储在 SQL 数据库中,并已经实现了以下的扫描器(scanner)和值接口(value interface):

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("无法将 %T 转换为 My struct", src)
}

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

有没有办法通过指针将参数传递给 Value() 函数?因为当尝试将数据转换回我的结构体时,我得到了一个 HugeParam 错误,表示传递给 Value() 函数的 attr 太大了。

任何建议都将不胜感激,谢谢!

更新:我尝试通过在代码中添加 nolint 来忽略 hugeParam,但错误仍然存在。以下是错误消息:

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)
英文:

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)

答案1

得分: 0

修复:

/nolint // hugeParam: //插入解释
func (attr mystruct) Value() (driver.Value, error) {
    return json.Marshal(attr)
}
英文:

Fix:

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

huangapple
  • 本文由 发表于 2022年6月6日 11:07:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/72512661.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定