drjmrg8766
2018-07-08 04:40
浏览 331
已采纳

如何使用reflect.TypeOf([] string {“ a”})。Elem()?

I'm really new to golang and I'm struggling with the basics. I wrote a piece of code like this:

package main
import (
  "log"
  "reflect"
)

if reflect.TypeOf([]string{"a"}).Elem() == reflect.String {
  log.Println("success")
}
if reflect.TypeOf([]int{1}).Elem() == reflect.Int{
  log.Println("success")
}
if reflect.TypeOf([]float64{1.00}).Elem() == reflect.Float64 {
  log.Println("success")
}

When I run this code, I get the error

invalid operation: reflect.TypeOf([]string literal).Elem() == reflect.String (mismatched types reflect.Type and reflect.Kind)

I don't understand the documentation https://golang.org/pkg/reflect/ because I can't find examples of how to reference the different "types" or "kinds"

How should I be writing my if statements to do the comparisons I'm attempting?

图片转代码服务由CSDN问答提供 功能建议

我真的是golang的新手,我在基础方面苦苦挣扎。 我写了一段这样的代码:

 包main 
import(
“ log” 
“ reflect” 
)
 
if Reflection.TypeOf([  ] string {“ a”})。Elem()== Reflection.String {
 log.Println(“ success”)
} 
ifreflect.TypeOf([] int {1})。Elem()== 反射.Int {
 log.Println(“成功”)
} 
if反射.TypeOf([] float64 {1.00})。Elem()==反射.Float64 {
 log.Println(“成功”)  
} 
   
 
 

运行此代码时,出现错误

无效操作:反映 .TypeOf([]字符串文字).Elem()== Reflection.String(类型不匹配的reflect.Type和reflect.Kind)

无法理解文档 https://golang.org/pkg/reflect/ ,因为我做不到 查找有关如何引用不同“类型”或“种类”的示例

我应该如何编写我的if语句来进行比较? < / DIV>

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • doudou201701 2018-07-08 04:43
    已采纳

    reflect.Type is an interface with a method called Kind(). As per document:

        // Kind returns the specific kind of this type.
        Kind() Kind
    

    So you should write :

    if reflect.TypeOf([]string{"a"}).Elem().Kind() == reflect.String {
      log.Println("success")
    }
    
    点赞 打赏 评论
  • douxin5953 2018-07-08 06:34

    To compare types, use:

    if reflect.TypeOf([]string{"a"}).Elem() == reflect.TypeOf("") {
      log.Println("success")
    }
    

    To compare kinds, use:

    if reflect.TypeOf([]string{"a"}).Elem().Kind() == reflect.String {
      log.Println("success")
    }
    

    If you want to test for a specific type, then compare types. If you want to determine what sort of type it is, then compare kinds.

    This example might help:

    type x string
    

    The x and string types are both kinds of string. The kind comparison returns true for both:

    fmt.Println(reflect.TypeOf(x("")).Kind() == reflect.String) // prints true
    fmt.Println(reflect.TypeOf("").Kind() == reflect.String) // prints true
    

    The x and string types are distinct:

    fmt.Println(reflect.TypeOf(x("")) == reflect.TypeOf(""))    // prints false
    
    点赞 打赏 评论
  • dongzhuo1498 2018-07-08 10:32

    For simple type comparisons like what you're showing, you don't need reflection. You can use a type assertion instead:

    stuff := []interface{}{"hello", 1, nil}
    for _, obj := range stuff {
            if _, ok := obj.(string); ok {
                    fmt.Printf("is a string
    ")
            } else if _, ok := obj.(int); ok {
                    fmt.Printf("is an int
    ")
            } else {
                    fmt.Printf("is something else
    ")
            }
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题