使用Golang设置reflect.Value的方法是通过sql.NullString。

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

Golang set reflect.Value by sql.NullString

问题

如何通过inData.SourceId(类型为String)设置item.SourceId(类型为sql.NullString)?我不知道如何在红色块中编写代码。

我在https://stackoverflow.com/questions/6395076/using-reflect-how-do-you-set-the-value-of-a-struct-field 中找到了reflect.ValueOf(&foo).Elem().Field(0).SetInt(321)。是否有类似于SetInt用于sql.NullString的方法?

  1. type InDataType struct {
  2. Id string
  3. SourceId string
  4. }
  5. type ItemType struct {
  6. Id string
  7. SourceId sql.NullString
  8. }
  9. setField(item, inData, "SourceId")
  10. func setField(item interface{}, inData interface{}, fieldName string) {
  11. // t := reflect.TypeOf(inData)
  12. // fmt.Println(t)
  13. itemValue := reflect.ValueOf(item).Elem().FieldByName(fieldName)
  14. itemType := reflect.ValueOf(item).Elem().FieldByName(fieldName).Type().String()
  15. fmt.Println(itemType, ",", itemValue)
  16. inDataValue := reflect.ValueOf(inData).Elem().FieldByName(fieldName)
  17. inDataType := reflect.ValueOf(inData).Elem().FieldByName(fieldName).Type().String()
  18. fmt.Println(inDataType, ",", inDataValue)
  19. if itemType == "sql.NullString" {
  20. // itemValue = sql.NullString{String: inDataValue.Value().String(), Valid: inDataValue.String() != "";}
  21. }
  22. }

你可以尝试使用以下代码来设置item.SourceId

  1. if itemType == "sql.NullString" {
  2. itemValue.Set(reflect.ValueOf(sql.NullString{String: inDataValue.String(), Valid: inDataValue.String() != ""}))
  3. }

这将根据inData.SourceId的值设置item.SourceId。请确保在使用此代码之前导入"database/sql""reflect"包。

英文:

How can I set item.SourceId(the type is sql.NullString) by inData.SourceId(the type is String) ?
I don't know how to write the code in the red block
使用Golang设置reflect.Value的方法是通过sql.NullString。

I found reflect.ValueOf(&foo).Elem().Field(0).SetInt(321) in https://stackoverflow.com/questions/6395076/using-reflect-how-do-you-set-the-value-of-a-struct-field. Is there something like SetInt for sql.NullString ?

  1. type InDataType struct {
  2. Id string
  3. SourceId string
  4. }
  5. type ItemType struct {
  6. Id string
  7. SourceId sql.NullString
  8. }
  9. setField(item, inData, "SourceId")
  10. func setField(item interface{}, inData interface{}, fieldName string) {
  11. // t := reflect.TypeOf(inData)
  12. // fmt.Println(t)
  13. itemValue := reflect.ValueOf(item).Elem().FieldByName(fieldName)
  14. itemType := reflect.ValueOf(item).Elem().FieldByName(fieldName).Type().String()
  15. fmt.Println(itemType, ",", itemValue)
  16. inDataValue := reflect.ValueOf(inData).Elem().FieldByName(fieldName)
  17. inDataType := reflect.ValueOf(inData).Elem().FieldByName(fieldName).Type().String()
  18. fmt.Println(inDataType, ",", inDataValue)
  19. if itemType == "sql.NullString" {
  20. // itemValue = sql.NullString{String: inDataValue.Value().String(), Valid: inDataValue.String() != ""}
  21. }
  22. }

答案1

得分: 0

如果 itemType == "sql.NullString" {
itemValue.Set(reflect.ValueOf(sql.NullString{String: inDataValue.String(), Valid: inDataValue.String() != ""}))
}

英文:
  1. if itemType == "sql.NullString" {
  2. itemValue.Set(reflect.ValueOf(sql.NullString{String: inDataValue.String(), Valid: inDataValue.String() != ""}))
  3. }

huangapple
  • 本文由 发表于 2022年10月31日 15:30:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/74259991.html
匿名

发表评论

匿名网友

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

确定