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

如何使用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?

  • 写回答

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
    ")
            }
    }
    
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 我不明白为什么c#微软的官方api浏览器为什么不支持函数说明的检索,有支持检索函数说明的工具吗?
  • ¥15 ORBSLAM2框架跑ICL-NUIM数据集
  • ¥15 在我想检测ros是否成功安装时输入roscore出现以下
  • ¥30 老板让我做一个公司的投屏,实时显示日期,时间,安全生产的持续天数,完全没头绪啊
  • ¥15 Google Chrome 所有页面崩溃,三种解决方案都没有解决,我崩溃了
  • ¥20 使用uni-app发起网络请求,获取重定向302返回的cookie
  • ¥20 手机外部浏览器拉起微信小程序支付 (相关搜索:微信小程序)
  • ¥20 怎样通过一个网址找到其他同样模版的网址
  • ¥30 XIAO esp32c3 读取FDC2214的数据
  • ¥15 在工控机(Ubuntu系统)上外接USB蓝牙硬件进行蓝牙通信