首页 > 其他 > 详细

Go之类型判断

时间:2015-10-10 10:19:12      阅读:247      评论:0      收藏:0      [点我收藏+]
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")
	}

  

Go之类型判断

原文:http://www.cnblogs.com/anbylau2130/p/4865833.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!