cc *_*ung 105 types type-conversion go
我没有找到使用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)
}
Run Code Online (Sandbox Code Playgroud)
你知道使用Go的好介绍interface{}吗?
具体问题:
the*_*mue 111
你也可以做类型开关:
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.")
}
Run Code Online (Sandbox Code Playgroud)
pet*_*rSO 84
你的例子确实有效.这是一个简化版本.
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
Run Code Online (Sandbox Code Playgroud)
它使用Type断言.
new*_*cct 46
您可以使用reflection(reflect.TypeOf())来获取某事物的类型,并且它给出的值(Type)具有String您可以打印的字符串表示(方法).
h4c*_*1k3 14
下面是使用开关和反射解码通用映射的示例,因此如果您不匹配该类型,请使用反射来计算它,然后在下次添加该类型.
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)
}
}
Run Code Online (Sandbox Code Playgroud)
有多种方法可以获取类型的字符串表示形式。开关还可以与用户类型一起使用:
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")
}
Run Code Online (Sandbox Code Playgroud)
游乐场链接:https://play.golang.org/p/VDeNDUd9uK6
类型开关也可以与反射材料一起使用:
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")
}
Run Code Online (Sandbox Code Playgroud)