github.com/go4org/go4@v0.0.0-20200104003542-c7e774b10ea0/reflectutil/reflectutil.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package reflectutil contains reflect utilities. 6 package reflectutil 7 8 import "reflect" 9 10 // hasPointers reports whether the given type contains any pointers, 11 // including any internal pointers in slices, funcs, maps, channels, 12 // etc. 13 // 14 // This function exists for Swapper's internal use, instead of reaching 15 // into the runtime's *reflect._rtype kind&kindNoPointers flag. 16 func hasPointers(t reflect.Type) bool { 17 if t == nil { 18 panic("nil Type") 19 } 20 k := t.Kind() 21 if k <= reflect.Complex128 { 22 return false 23 } 24 switch k { 25 default: 26 // chan, func, interface, map, ptr, slice, string, unsafepointer 27 // And anything else. It's safer to err on the side of true. 28 return true 29 case reflect.Array: 30 return hasPointers(t.Elem()) 31 case reflect.Struct: 32 num := t.NumField() 33 for i := 0; i < num; i++ { 34 if hasPointers(t.Field(i).Type) { 35 return true 36 } 37 } 38 return false 39 } 40 }