Home > Backend Development > Golang > How Can We Identify Non-Builtin Types in Go Using Reflection Without Type Listing?

How Can We Identify Non-Builtin Types in Go Using Reflection Without Type Listing?

Mary-Kate Olsen
Release: 2024-12-27 13:12:12
Original
579 people have browsed it

How Can We Identify Non-Builtin Types in Go Using Reflection Without Type Listing?

Identifying Non-Builtin Types Using Reflect

Question:

Differentiating between types such as []byte and type A []byte using reflect is a common challenge. How can we distinguish between these types without relying on a defined list of types to check against?

Introduction:

Background on Types:
Understanding the classification of types in Go is crucial. There are two main categories: named and unnamed types. Named types are declared using the type keyword, while unnamed types are created using type literals. Predeclared types, such as int and string, are also available for immediate use.

Using Type.Name() and Type.PkgPath():

Type.Name() returns the name of a named type, while Type.PkgPath() returns the package path for both named and predeclared types. This allows us to separate unnamed types (empty Type.Name()) from named and predeclared types.

Special Cases:

Anonymous struct types and map types require special consideration. Anonymous struct types can contain fields of custom types, so we must check the fields recursively using Type.Elem(). Similarly, for maps, we must check both the key and value types.

Example Implementation:

Here's a code example that differentiates between builtin and non-builtin types:

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
}
Copy after login

Usage:

We can test the isCustom() function with various types:

fmt.Println(isCustom(reflect.TypeOf(int(2))))            // false
fmt.Println(isCustom(reflect.TypeOf(A{})))               // true
fmt.Println(isCustom(reflect.TypeOf(map[K]int{})))       // true
Copy after login

The above is the detailed content of How Can We Identify Non-Builtin Types in Go Using Reflection Without Type Listing?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template