Go之类型判断

boy := util.Boy{util.Person{"Eric", 19, "boy"}, "1"}


var boyClone interface{} = boy

  如何判断 boyClone是否是boy类型呢?

if i, ok := boyClone.(util.Boy); ok {
	fmt.Println(i)
}

 

如果判断多个类型,可以如下书写:

switch v := boyClone.(type) {

	case nil:
		fmt.Println("nil")
	case fmt.Stringer:
		fmt.Println(v)
	case func() string:
		fmt.Println(v())
	case util.Boy:
		fmt.Println(v)
	default:
		fmt.Println("unknow")
	}

  

posted @ 2015-10-10 09:33  尼姑哪里跑  阅读(251)  评论(0编辑  收藏  举报