syntaxgo_reflect

package
v0.0.54 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2025 License: MIT Imports: 4 Imported by: 5

Documentation

Overview

Package syntaxgo_reflect provides type reflection and package path extraction Extract package info from Go types using reflection API Generate qualified type names and import statements to use in code generation

syntaxgo_reflect 包提供类型反射和包路径提取 使用反射 API 从 Go 类型提取包信息 生成限定类型名和导入语句用于代码生成

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateTypeUsageCode

func GenerateTypeUsageCode(a reflect.Type) string

GenerateTypeUsageCode generates the code for using a type from another package. It constructs the code representation for the type as it would be used in another package, including the package name and type name. If the type is from the same package, it returns just the type name.

For example, if the type is "Demo" from package "abc", this function will return "abc.Demo". If the package path is empty, it just returns the type name.

GenerateTypeUsageCode 用于生成从其他包调用某个包类型的代码。 它构造了类型在其他包中的使用代码,包括包名和类型名。 如果类型来自相同的包,则只返回类型名。

举个例子,如果类型是来自包 "abc" 的 "Demo",这个函数将返回 "abc.Demo"。 如果包路径为空,则只返回类型名。

func GetObject

func GetObject[T any]() (a T)

GetObject returns a zero value of type T Compile-time prevents T from being a pointer type (like *A)

GetObject 返回类型 T 的零值 在编译阶段预防 T 是指针类型(比如 *A)的情况

func GetPkgName

func GetPkgName(a any) string

GetPkgName extracts the package name from the package path Returns the base name of the package path (last segment)

GetPkgName 从包路径提取包名 返回包路径的基础名称(最后一段)

func GetPkgNameV2

func GetPkgNameV2[T any]() string

GetPkgNameV2 is a generic version of GetPkgName Returns the package name using generic type param

GetPkgNameV2 是 GetPkgName 的泛型版本 使用泛型类型参数返回包名

func GetPkgNameV3

func GetPkgNameV3(a any) string

GetPkgNameV3 extracts package name adapting to both objects and pointers Handles both value types and pointer types in the same way

GetPkgNameV3 提取包名,适配对象和指针 以相同方式处理值类型和指针类型

func GetPkgNameV4

func GetPkgNameV4[T any](p *T) string

GetPkgNameV4 extracts package name from a pointer using generics Dereferences the pointer to get the underlying type's package name

GetPkgNameV4 使用泛型从指针提取包名 解引用指针以获取底层类型的包名

func GetPkgPath

func GetPkgPath(a any) string

GetPkgPath returns the package path of the type using reflection Note: Works with struct objects but not with pointers/functions/methods Functions cannot be reflected to get package path - use direct code reference instead

GetPkgPath 使用反射返回类型的包路径 注意:适用于结构体对象但不适用于指针/函数/方法类型 函数无法通过反射获取包路径 - 请使用直接代码引用

func GetPkgPathV2

func GetPkgPathV2[T any]() string

GetPkgPathV2 is a generic version of GetPkgPath Both versions are useful: non-generic when passed from non-generic callers Generic version adds type safety using just V2 suffix

GetPkgPathV2 是 GetPkgPath 的泛型版本 两个版本都有用:非泛型版用于非泛型调用者传递对象时 泛型版本通过 V2 后缀增加类型安全性

func GetPkgPathV3

func GetPkgPathV3(a any) string

GetPkgPathV3 returns package path adapting to both objects and pointers Handles both value types and pointer types in the same way

GetPkgPathV3 返回包路径,适配对象和指针 以相同方式处理值类型和指针类型

func GetPkgPathV4

func GetPkgPathV4[T any](p *T) string

GetPkgPathV4 returns package path from a pointer using generics Dereferences the pointer to get the underlying type's package path

GetPkgPathV4 使用泛型从指针返回包路径 解引用指针以获取底层类型的包路径

func GetPkgPaths

func GetPkgPaths(objectsTypes []reflect.Type) []string

GetPkgPaths returns the package paths of provided reflect.Type objects. GetPkgPaths 返回给定 reflect.Type 对象的包路径。

func GetQuotedPackageImportPaths

func GetQuotedPackageImportPaths(objectsTypes []reflect.Type) []string

GetQuotedPackageImportPaths generates the import paths for the provided types, with the necessary quotes for import statements. GetQuotedPackageImportPaths 生成给定类型的导入路径,再为导入语句加上必要的双引号。

func GetType

func GetType(a any) reflect.Type

GetType returns the reflect.Type of the provided object. GetType 返回提供对象的 reflect.Type。

func GetTypeName

func GetTypeName(a any) string

GetTypeName returns the name of the type Returns the simple type name without package path

GetTypeName 返回类型名称 返回不带包路径的简单类型名称

func GetTypeNameV2

func GetTypeNameV2[T any]() string

GetTypeNameV2 is a generic version of GetTypeName Returns the type name using generic type param

GetTypeNameV2 是 GetTypeName 的泛型版本 使用泛型类型参数返回类型名称

func GetTypeNameV3

func GetTypeNameV3(object any) string

GetTypeNameV3 returns type name adapting to both objects and pointers Handles both value types and pointer types in the same way

GetTypeNameV3 返回类型名称,适配对象和指针 以相同方式处理值类型和指针类型

func GetTypeNameV4

func GetTypeNameV4[T any](p *T) string

GetTypeNameV4 returns type name from a pointer using generics Dereferences the pointer to get the underlying type name

GetTypeNameV4 使用泛型从指针返回类型名称 解引用指针以获取底层类型名称

func GetTypeV2

func GetTypeV2[T any]() reflect.Type

GetTypeV2 is a generic version of GetType. It returns the reflect.Type of the result from GetObject[T](). GetTypeV2 是 GetType 的泛型版本,返回 GetObject[T]() 结果的 reflect.Type。

func GetTypeV3

func GetTypeV3(object any) reflect.Type

GetTypeV3 returns the reflect.Type of an object. It checks if the object is a pointer, and if so, it returns the type of the underlying value. GetTypeV3 获取对象的 reflect.Type。如果对象是指针,返回指针所指向的值的类型。

func GetTypeV4

func GetTypeV4[T any](p *T) reflect.Type

GetTypeV4 is a generic function that takes a pointer and returns the reflect.Type of the dereferenced value. GetTypeV4 是一个泛型函数,接受一个指针并返回解引用后的值的 reflect.Type。

func GetTypes

func GetTypes(objects []any) []reflect.Type

GetTypes extracts the reflect.Type for each objects. GetTypes 从给定的对象列表中提取每个对象的 reflect.Type。

func NewObject

func NewObject[T any]() (a *T)

NewObject creates a new pointer to type T Returns a pointer to a fresh allocated zero value of type T

NewObject 创建类型 T 的新指针 返回指向新分配的类型 T 零值的指针

func NewStructTag added in v0.0.53

func NewStructTag(tag string) reflect.StructTag

NewStructTag creates a reflect.StructTag from a string Converts a tag string into a reflect.StructTag type that can be queried

NewStructTag 从字符串创建 reflect.StructTag 将标签字符串转换为可查询的 reflect.StructTag 类型

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL