将 reflect.Value 设置为 Go 中的切片

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

Set reflect.Value to slice in Go

问题

我尝试保存一个整数字段的切片。该字段的类型是reflect.Value。

我遇到了错误:无法将articles(类型为[]int64的变量)作为参数传递给field.Set中的reflect.Value值。我该怎么解决这个问题?

非常感谢!

  1. for i := 0; i < elem.Type().NumField(); i++ {
  2. structField := elem.Type().Field(i)
  3. tag := structField.Tag.Get("db")
  4. fieldType := structField.Type
  5. fieldName := structField.Name
  6. val, ok := record.Get(fmt.Sprintf("%s", tag))
  7. if ok {
  8. // 忽略空值
  9. if val == nil {
  10. continue
  11. }
  12. field := elem.FieldByName(fieldName)
  13. if field.IsValid() {
  14. t := fieldType.String()
  15. switch t {
  16. case "string":
  17. field.SetString(val.(string))
  18. case "int64":
  19. field.SetInt(val.(int64))
  20. case "float64":
  21. field.SetFloat(val.(float64))
  22. case "boolean":
  23. field.SetBool(val.(bool))
  24. case "[]int64":
  25. articles := []int64{}
  26. initData := []interface{}{
  27. val,
  28. }
  29. for _, data := range initData {
  30. for _, v := range data.([]interface{}) {
  31. t := v
  32. articles = append(articles, t.(int64))
  33. }
  34. }
  35. //
  36. field.Set(articles)
  37. default:
  38. return fmt.Errorf("Invalid type: %s", t)
  39. }
  40. }
  41. }
  42. }

将 reflect.Value 设置为 Go 中的切片

英文:

I try to save a slice of integers of a field. the field is type reflect.Value.

I get the error: cannot use articles (variable of type []int64) as reflect.Value value in argument to field.Set. What can I do to encounter that?

Thank you very much!

  1. for i := 0; i &lt; elem.Type().NumField(); i++ {
  2. structField := elem.Type().Field(i)
  3. tag := structField.Tag.Get(&quot;db&quot;)
  4. fieldType := structField.Type
  5. fieldName := structField.Name
  6. val, ok := record.Get(fmt.Sprintf(&quot;%s&quot;, tag))
  7. if ok {
  8. // Ignore nil values
  9. if val == nil {
  10. continue
  11. }
  12. field := elem.FieldByName(fieldName)
  13. if field.IsValid() {
  14. t := fieldType.String()
  15. switch t {
  16. case &quot;string&quot;:
  17. field.SetString(val.(string))
  18. case &quot;int64&quot;:
  19. field.SetInt(val.(int64))
  20. case &quot;float64&quot;:
  21. field.SetFloat(val.(float64))
  22. case &quot;boolean&quot;:
  23. field.SetBool(val.(bool))
  24. case &quot;[]int64&quot;:
  25. articles := []int64{}
  26. initData := []interface{}{
  27. val,
  28. }
  29. for _, data := range initData {
  30. for _, v := range data.([]interface{}) {
  31. t := v
  32. articles = append(articles, t.(int64))
  33. }
  34. }
  35. //
  36. field.Set(articles)
  37. default:
  38. return fmt.Errorf(&quot;Invalid type: %s&quot;, t)
  39. }
  40. }
  41. }

将 reflect.Value 设置为 Go 中的切片

答案1

得分: 1

mkopriva的答案是:field.Set(reflect.ValueOf(articles))

英文:

The answer of mkopriva: field.Set(reflect.ValueOf(articles))

huangapple
  • 本文由 发表于 2022年6月25日 18:27:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/72753091.html
匿名

发表评论

匿名网友

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

确定