就像OO编程中,一个函数名,支持多种参数一样
但这里只需要1个参数
你需要用到switch和类型检测; 参加下面的check函数如何处理传入的value分别为error,string和bool
3种不同类型的.
func check(value interface{}) {
/*
This is an assert function;
input:
<value> value can be bool, error or string
output:
check will panic when value is false(bool) or error(not nil) or string(not empty),
however NOTHING will happen when value is nil
*/
if value == nil {
return
}
switch v := value.(type) {
case error:
e, _ := value.(error)
if e != nil {
panic(e)
}
case bool:
b, _ := value.(bool)
if b == false {
panic("Abort!")
}
case string:
s, _ := value.(string)
if len(s) > 0 {
panic(s)
}
default:
panic(v)
}
}
interface{}, 使用反射判断interface类型。