github.com/fengyoulin/inspect@v0.2.1/inspect.go (about)

     1  package inspect
     2  
     3  import (
     4  	"reflect"
     5  	"unsafe"
     6  )
     7  
     8  // TypeOf find the type by package path and name
     9  func TypeOf(pathName string) reflect.Type {
    10  	if typ, ok := types[pathName]; ok {
    11  		return typ
    12  	}
    13  	return nil
    14  }
    15  
    16  // Types return all types in typelinks
    17  func Types() (ts [][]reflect.Type) {
    18  	typ := reflect.TypeOf(0)
    19  	face := (*iface)(unsafe.Pointer(&typ))
    20  	sections, offset := typelinks()
    21  	ts = make([][]reflect.Type, 0, len(offset))
    22  	for i, offs := range offset {
    23  		section := sections[i]
    24  		list := make([]reflect.Type, 0, len(offs))
    25  		for _, off := range offs {
    26  			face.data = unsafe.Pointer(uintptr(section) + uintptr(off))
    27  			list = append(list, typ)
    28  		}
    29  		ts = append(ts, list)
    30  	}
    31  	return
    32  }