github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/types/type_id.go (about) 1 package types 2 3 import ( 4 "reflect" 5 ) 6 7 type TypeId string 8 9 func (t TypeId) String() string { return string(t) } 10 11 func typeid(value interface{}) string { 12 rt := reflect.TypeOf(value) 13 name := rt.String() 14 15 // But for named types (or pointers to them), qualify with import path (but see inner comment). 16 // Dereference one pointer looking for a named type. 17 star := "" 18 if rt.Name() == "" { 19 if pt := rt; pt.Kind() == reflect.Ptr { 20 star = "*" 21 // NOTE: The following line should be rt = pt.Elem() to implement 22 // what the comment above claims, but fixing it would break compatibility 23 // with existing gobs. 24 // 25 // Given package p imported as "full/p" with these definitions: 26 // package p 27 // type T1 struct { ... } 28 // this table shows the intended and actual strings used by gob to 29 // name the types: 30 // 31 // Type Correct string Actual string 32 // 33 // T1 full/p.T1 full/p.T1 34 // *T1 *full/p.T1 *full/p.T1 35 // 36 //rt = pt 37 rt = pt.Elem() 38 } 39 } 40 if rt.Name() != "" { 41 if rt.PkgPath() == "" { 42 name = star + rt.Name() 43 } else { 44 name = star + rt.PkgPath() + "." + rt.Name() 45 } 46 } 47 return name 48 } 49 50 type typeof struct{} 51 52 func (typeof) Typeof(value interface{}) TypeId { return TypeId(typeid(value)) } 53 54 var Type = typeof{} 55 56 func Typeof(value interface{}) TypeId { return Type.Typeof(value) }