Home >Backend Development >Golang >How Can I Distinguish Between Built-in and Custom Types Using Go's `reflect` Package?
Identifying Non Built-in Types Using Reflect
The Challenge
You need to distinguish types like type A []byte from []byte using the reflect package. reflect.TypeOf(A{}).Kind returns Slice for both types, making it challenging to discern them.
Background on Types
Approach
Using the methods of the reflect type:
Special Cases
Implementation
func isCustom(t reflect.Type) bool { if t.PkgPath() != "" { return true } if k := t.Kind(); k == reflect.Array || k == reflect.Chan || k == reflect.Map || k == reflect.Ptr || k == reflect.Slice { return isCustom(t.Elem()) || k == reflect.Map && isCustom(t.Key()) } else if k == reflect.Struct { for i := t.NumField() - 1; i >= 0; i-- { if isCustom(t.Field(i).Type) { return true } } } return false }
Testing
Applying this to various types:
fmt.Println(isCustom(reflect.TypeOf(""))) // false fmt.Println(isCustom(reflect.TypeOf(int(2)))) // false fmt.Println(isCustom(reflect.TypeOf([]int{}))) // false fmt.Println(isCustom(reflect.TypeOf(struct{ i int }{}))) // false fmt.Println(isCustom(reflect.TypeOf(&i))) // false fmt.Println(isCustom(reflect.TypeOf(map[string]int{}))) // false fmt.Println(isCustom(reflect.TypeOf(A{}))) // true fmt.Println(isCustom(reflect.TypeOf(&A{}))) // true fmt.Println(isCustom(reflect.TypeOf([]A{}))) // true fmt.Println(isCustom(reflect.TypeOf([][]A{}))) // true fmt.Println(isCustom(reflect.TypeOf(struct{ a A }{}))) // true fmt.Println(isCustom(reflect.TypeOf(map[K]int{}))) // true fmt.Println(isCustom(reflect.TypeOf(map[string]K{}))) // true
This demonstrates the ability to differentiate between built-in and custom types using the reflect package effectively.
The above is the detailed content of How Can I Distinguish Between Built-in and Custom Types Using Go's `reflect` Package?. For more information, please follow other related articles on the PHP Chinese website!