cxfans
11/1/2019 - 6:39 AM

利⽤类型推断,判断接⼝对象是否某个具体的接⼝或类型

package main

import "fmt"

type User struct {
	id   int
	name string
}

func main() {
	var o interface{} = &User{1, "Tom"}

	if i, ok := o.(fmt.Stringer); ok {
		fmt.Println(i)
	}

	u := o.(*User)
	fmt.Println(u)

	v, ok := o.(fmt.Stringer)
	fmt.Println(v, ok)
}