Go convert reflect.Value from string to int

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

Go convert reflect.Value from string to int

问题

我遇到了这样的错误:

  1. reflect.Value.Convert: 无法将类型为 string 的值转换为类型 int
  2. goroutine 6

当我运行以下代码时:

  1. param := "1" // 类型为 string
  2. ps := fn.In(i) // 类型为 int
  3. if !reflect.TypeOf(param).ConvertibleTo(ps) {
  4. fmt.Print("无法转换参数。\n") // 这行代码被打印出来
  5. }
  6. convertedParam := reflect.ValueOf(param).Convert(ps)

有没有办法在不创建 switch/case 和多行代码的情况下完成这个操作?我只是想找到最简单/最好的方法来做到这一点。

英文:

I am getting an error like this

  1. reflect.Value.Convert: value of type string cannot be converted to type int
  2. goroutine 6

When I am running this code

  1. param := "1" // type string
  2. ps := fn.In(i) // type int
  3. if !reflect.TypeOf(param).ConvertibleTo(ps) {
  4. fmt.Print("Could not convert parameter.\n") // this is printed
  5. }
  6. convertedParam := reflect.ValueOf(param).Convert(ps)

Can I do this in some way without creating a switch/case and multiple lines of code for converting to each type?
I am just looking for the easiest/best way to do this.

答案1

得分: 3

有关type conversion有特定的规则。字符串不能转换为数值。

使用strconv包从字符串中读取数值。

英文:

There are specific rules for type conversion. Strings cannot be converted to numeric values.

Use the strconv package to read a numeric value from a string.

答案2

得分: 0

你可以在循环中使用switch语句通过反射值来返回reflect.Value kind(),以返回该值的确切类型。

  1. v := reflect.ValueOf(s interface{})
  2. for t := 0; i < v.NumField(); i++ {
  3. fmt.Println(v.Field(i)) // 打印接口中索引处的值
  4. switch t := v.Kind() {
  5. case bool:
  6. fmt.Printf("布尔类型 %t\n", t) // t 的类型为 bool
  7. case int:
  8. fmt.Printf("整数类型 %d\n", t) // t 的类型为 int
  9. case *bool:
  10. fmt.Printf("指向布尔类型的指针 %t\n", *t) // t 的类型为 *bool
  11. case *int:
  12. fmt.Printf("指向整数类型的指针 %d\n", *t) // t 的类型为 *int
  13. default:
  14. fmt.Printf("未知类型 %T\n", t) // %T 打印 t 的类型
  15. }
  16. }

要将接口中的类型转换为另一种类型,首先将其值提取到一个变量中,然后使用类型转换将该值转换为所需类型。

英文:

You can use switch in loop through the reflect values which will return the reflect.Value kind() to return the exact type of that value

  1. v := reflect.ValueOf(s interface{})
  2. for t := 0; i &lt; v.NumField(); i++ {
  3. fmt.Println(v.Field(i)) // it will prints the value at index in interface
  4. switch t := v.Kind() {
  5. case bool:
  6. fmt.Printf(&quot;boolean %t\n&quot;, t) // t has type bool
  7. case int:
  8. fmt.Printf(&quot;integer %d\n&quot;, t) // t has type int
  9. case *bool:
  10. fmt.Printf(&quot;pointer to boolean %t\n&quot;, *t) // t has type *bool
  11. case *int:
  12. fmt.Printf(&quot;pointer to integer %d\n&quot;, *t) // t has type *int
  13. default:
  14. fmt.Printf(&quot;unexpected type %T\n&quot;, t) // %T prints whatever type t has
  15. }
  16. }

To convert a type in interface to another type first fetch the value of it in a variable and then use type conversions to convert the value

huangapple
  • 本文由 发表于 2015年12月16日 06:12:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/34300397.html
匿名

发表评论

匿名网友

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

确定