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 ¶
- func GenerateTypeUsageCode(a reflect.Type) string
- func GetObject[T any]() (a T)
- func GetPkgName(a any) string
- func GetPkgNameV2[T any]() string
- func GetPkgNameV3(a any) string
- func GetPkgNameV4[T any](p *T) string
- func GetPkgPath(a any) string
- func GetPkgPathV2[T any]() string
- func GetPkgPathV3(a any) string
- func GetPkgPathV4[T any](p *T) string
- func GetPkgPaths(objectsTypes []reflect.Type) []string
- func GetQuotedPackageImportPaths(objectsTypes []reflect.Type) []string
- func GetType(a any) reflect.Type
- func GetTypeName(a any) string
- func GetTypeNameV2[T any]() string
- func GetTypeNameV3(object any) string
- func GetTypeNameV4[T any](p *T) string
- func GetTypeV2[T any]() reflect.Type
- func GetTypeV3(object any) reflect.Type
- func GetTypeV4[T any](p *T) reflect.Type
- func GetTypes(objects []any) []reflect.Type
- func NewObject[T any]() (a *T)
- func NewStructTag(tag string) reflect.StructTag
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GenerateTypeUsageCode ¶
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 ¶
GetPkgName extracts the package name from the package path Returns the base name of the package path (last segment)
GetPkgName 从包路径提取包名 返回包路径的基础名称(最后一段)
func GetPkgNameV2 ¶
GetPkgNameV2 is a generic version of GetPkgName Returns the package name using generic type param
GetPkgNameV2 是 GetPkgName 的泛型版本 使用泛型类型参数返回包名
func GetPkgNameV3 ¶
GetPkgNameV3 extracts package name adapting to both objects and pointers Handles both value types and pointer types in the same way
GetPkgNameV3 提取包名,适配对象和指针 以相同方式处理值类型和指针类型
func GetPkgNameV4 ¶
GetPkgNameV4 extracts package name from a pointer using generics Dereferences the pointer to get the underlying type's package name
GetPkgNameV4 使用泛型从指针提取包名 解引用指针以获取底层类型的包名
func GetPkgPath ¶
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 ¶
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 ¶
GetPkgPathV3 returns package path adapting to both objects and pointers Handles both value types and pointer types in the same way
GetPkgPathV3 返回包路径,适配对象和指针 以相同方式处理值类型和指针类型
func GetPkgPathV4 ¶
GetPkgPathV4 returns package path from a pointer using generics Dereferences the pointer to get the underlying type's package path
GetPkgPathV4 使用泛型从指针返回包路径 解引用指针以获取底层类型的包路径
func GetPkgPaths ¶
GetPkgPaths returns the package paths of provided reflect.Type objects. GetPkgPaths 返回给定 reflect.Type 对象的包路径。
func GetQuotedPackageImportPaths ¶
GetQuotedPackageImportPaths generates the import paths for the provided types, with the necessary quotes for import statements. GetQuotedPackageImportPaths 生成给定类型的导入路径,再为导入语句加上必要的双引号。
func GetType ¶
GetType returns the reflect.Type of the provided object. GetType 返回提供对象的 reflect.Type。
func GetTypeName ¶
GetTypeName returns the name of the type Returns the simple type name without package path
GetTypeName 返回类型名称 返回不带包路径的简单类型名称
func GetTypeNameV2 ¶
GetTypeNameV2 is a generic version of GetTypeName Returns the type name using generic type param
GetTypeNameV2 是 GetTypeName 的泛型版本 使用泛型类型参数返回类型名称
func GetTypeNameV3 ¶
GetTypeNameV3 returns type name adapting to both objects and pointers Handles both value types and pointer types in the same way
GetTypeNameV3 返回类型名称,适配对象和指针 以相同方式处理值类型和指针类型
func GetTypeNameV4 ¶
GetTypeNameV4 returns type name from a pointer using generics Dereferences the pointer to get the underlying type name
GetTypeNameV4 使用泛型从指针返回类型名称 解引用指针以获取底层类型名称
func GetTypeV2 ¶
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 ¶
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 ¶
GetTypeV4 is a generic function that takes a pointer and returns the reflect.Type of the dereferenced value. GetTypeV4 是一个泛型函数,接受一个指针并返回解引用后的值的 reflect.Type。
func GetTypes ¶
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
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.