Home >Backend Development >Golang >How Can We Distinguish Between Built-in and Custom Types in Go Using Reflection?
Identifying Non-Builtin Types Using Reflection
Problem:
Differentiating between types like []byte and type A []byte using reflection proves challenging when reflect.TypeOf(A{}).Kind indicates both as slices of bytes. It becomes necessary to find a way to distinguish these types without relying on pre-defined lists of types.
Background:
Types in Go can be categorized as named (with a type name) or unnamed (using a type literal). Predeclared types (like string or int) are named, while types created using type literals (like []int or struct{i int}) are unnamed.
Solution:
Using Type.PkgPath():
Type.PkgPath() provides the package path of a named type. For predeclared or unnamed types, this path will be empty. Thus, non-builtin types can be identified by checking if Type.PkgPath() returns a non-empty string.
Handling Unnamed Types Derived from Custom Types:
Using type literals with custom types can result in unnamed types (e.g., []A). To determine if an unnamed type is derived from a custom type, check the element type using Type.Elem():
if elem := t.Elem(); elem.Kind() == reflect.Ptr || elem.Kind() == reflect.Struct { // Recursively check elem }
Special Cases:
Example Implementation:
The following code defines a utility function isCustom to identify non-builtin types:
func isCustom(t reflect.Type) bool { if t.PkgPath() != "" { return true } // Handle special cases and recursion for unnamed types return false }
Testing the Solution:
fmt.Println(isCustom(reflect.TypeOf(""))) // false (builtin) fmt.Println(isCustom(reflect.TypeOf(A{}))) // true (custom)
The above is the detailed content of How Can We Distinguish Between Built-in and Custom Types in Go Using Reflection?. For more information, please follow other related articles on the PHP Chinese website!