如何确定interface{}值的“真实”类型?

158

我还没有找到一个好的资源来学习如何使用 interface{} 类型。比如说:

package main

import "fmt"

func weirdFunc(i int) interface{} {
    if i == 0 {
        return "zero"
    }
    return i
}
func main() {
    var i = 5
    var w = weirdFunc(5)

    // this example works!
    if tmp, ok := w.(int); ok {
        i += tmp
    }

    fmt.Println("i =", i)
}

你知道一份介绍如何使用Go语言中的interface{}接口的好教程吗?

具体问题:

  • 如何获取w的“真实”类型?
  • 有没有办法获取类型的字符串表示形式?
  • 有没有办法使用类型的字符串表示形式来转换值?
8个回答

170
您也可以进行类型切换:
switch v := myInterface.(type) {
case int:
    // v is an int here, so e.g. v + 1 is possible.
    fmt.Printf("Integer: %v", v)
case float64:
    // v is a float64 here, so e.g. v + 1.0 is possible.
    fmt.Printf("Float64: %v", v)
case string:
    // v is a string here, so e.g. v + " Yeah!" is possible.
    fmt.Printf("String: %v", v)
default:
    // And here I'm feeling dumb. ;)
    fmt.Printf("I don't know, ask stackoverflow.")
}

谢谢你的回复。但还不太对。在这个例子中,我该如何将变量w强制转换为整数? - cc young
3
Mue的示例与if语句相同,但是使用了类型开关。在“case int”中,“v”将是一个整数;在“case float64”中,“v”将是一个float64等等。 - jimt
对了,我忘记了语法 var.(type),这个语法既狡猾又酷。 - cc young

121

你的示例确实有效。这是一个简化版本。

package main

import "fmt"

func weird(i int) interface{} {
    if i < 0 {
        return "negative"
    }
    return i
}

func main() {
    var i = 42
    if w, ok := weird(7).(int); ok {
        i += w
    }
    if w, ok := weird(-100).(int); ok {
        i += w
    }
    fmt.Println("i =", i)
}

Output:
i = 49

它使用类型断言(Type assertions)


你说得完全正确!谢谢!你对类型字符串表示有什么见解吗? - cc young
17
请查看reflect.TypeOf - Dmitri Goldring
@DmitriGoldring 至少回答了主题标题中的问题。这个答案没有。非常感谢。 - C4d

65
您可以使用反射(reflect.TypeOf())获取某物的类型,该方法返回的值(Type)具有字符串表示形式(String方法),您可以将其打印出来。

11
如果你只是想获取一个字符串或类型(例如在Mue的回答中打印默认块的类型开关链接),你可以直接使用fmt的"%T"格式,而不是直接使用reflect - Dave C

18
这里有一个通用地图解码的示例,使用了switch和reflection。如果您无法匹配类型,请使用reflection进行解决,然后在下一次添加类型。
var data map[string]interface {}

...

for k, v := range data {
    fmt.Printf("pair:%s\t%s\n", k, v)   

    switch t := v.(type) {
    case int:
        fmt.Printf("Integer: %v\n", t)
    case float64:
        fmt.Printf("Float64: %v\n", t)
    case string:
        fmt.Printf("String: %v\n", t)
    case bool:
        fmt.Printf("Bool: %v\n", t)
    case []interface {}:
        for i,n := range t {
            fmt.Printf("Item: %v= %v\n", i, n)
        }
    default:
        var r = reflect.TypeOf(t)
        fmt.Printf("Other:%v\n", r)             
    }
}

9

获取类型的字符串表示有多种方法。对于用户自定义类型,也可以使用开关:

var user interface{}
user = User{name: "Eugene"}

// .(type) can only be used inside a switch
switch v := user.(type) {
case int:
    // Built-in types are possible (int, float64, string, etc.)
    fmt.Printf("Integer: %v", v)
case User:
    // User defined types work as well  
    fmt.Printf("It's a user: %s\n", user.(User).name)
}

// You can use reflection to get *reflect.rtype
userType := reflect.TypeOf(user)
fmt.Printf("%+v\n", userType)

// You can also use %T to get a string value
fmt.Printf("%T", user)

// You can even get it into a string
userTypeAsString := fmt.Sprintf("%T", user)

if userTypeAsString == "main.User" {
    fmt.Printf("\nIt's definitely a user")
}

玩具场链接: https://play.golang.org/p/VDeNDUd9uK6


8

类型转换还可以与反射技术一起使用:

var str = "hello!"
var obj = reflect.ValueOf(&str)

switch obj.Elem().Interface().(type) {
case string:
    log.Println("obj contains a pointer to a string")
default:
    log.Println("obj contains something else")
}

4

你可以使用

  1. Printf("%T", interface{})
  2. reflect.TypeOf
  3. reflect.ValueOf(x).Kind()

-> reflect:

 func typeofobject(x interface{}){
        fmt.Println(reflect.TypeOf(x))
 }

-> Printf()

 // Golang program to find the variable type
    package main
    
    // importing required packages
    import (
      "fmt"
    )
    
    // main function
    func main() {
      f := true
      st := ""
      a := 1
      d := 1.0
      arr := []string{"Go", "Is", "Fun"}
    
      fmt.Printf("%T\n", f)
      fmt.Printf("%T\n", st)
      fmt.Printf("%T\n", a)
      fmt.Printf("%T\n", d)
      fmt.Printf("%T\n", arr)
    }

输出:

bool
string
int
float64
[]string

-> reflect.ValueOf(x).Kind())

func typeofobject(x interface{}){
    fmt.Println(reflect.ValueOf(x).Kind())
}

源代码:极客教程


3
我将提供一种方法,根据传递给本地类型接收器的反射 Kinds 参数返回布尔值(因为我找不到类似的东西)。首先,我们声明一个匿名类型,类型为 reflect.Value:
type AnonymousType reflect.Value

然后,我们为本地类型AnonymousType添加了一个构建器,它可以接受任何可能的类型(作为接口):

func ToAnonymousType(obj interface{}) AnonymousType {
    return AnonymousType(reflect.ValueOf(obj))
}

然后我们为我们的 AnonymousType 结构添加一个函数,该函数根据 reflect.Kind 进行断言:

func (a AnonymousType) IsA(typeToAssert reflect.Kind) bool {
    return typeToAssert == reflect.Value(a).Kind()
}

这使我们能够调用以下内容:
var f float64 = 3.4

anon := ToAnonymousType(f)

if anon.IsA(reflect.String) {
    fmt.Println("Its A String!")
} else if anon.IsA(reflect.Float32) {
    fmt.Println("Its A Float32!")
} else if anon.IsA(reflect.Float64) {
    fmt.Println("Its A Float64!")
} else {
    fmt.Println("Failed")
}

可以在这里查看更长且可工作的版本:https://play.golang.org/p/EIAp0z62B7


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接