SHARE
TWEET

Untitled

a guest Jul 18th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 不通过反射, 通过类型断言判断一个类型是否含有某函数
  2. package main
  3.  
  4. import (
  5.     "fmt"
  6. )
  7.  
  8. type A int
  9. type B int
  10.  
  11. func (b B) M(x int) string {
  12.     return fmt.Sprint(b, ": ", x)
  13. }
  14.  
  15. func check(v interface{}) bool {
  16.     _, has := v.(interface{M(int) string}) // 匿名接口
  17.     return has
  18. }
  19.  
  20. func main() {
  21.     var a A = 123
  22.     var b B = 789
  23.     fmt.Println(check(a), check(b)) // false true
  24. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top