types
49 分钟阅读
Package types declares the data types and implements the algorithms for type-checking of Go packages. Use Config.Check to invoke the type checker for a package. Alternatively, create a new type checker with NewChecker and invoke it incrementally by calling Checker.Files.
types 包声明数据类型并实现 Go 包类型检查的算法。使用 Config.Check 调用包的类型检查器。或者,使用 NewChecker 创建一个新的类型检查器,并通过调用 Checker.Files 增量调用它。
Type-checking consists of several interdependent phases:
类型检查包含几个相互依赖的阶段:
Name resolution maps each identifier (ast.Ident) in the program to the language object (Object) it denotes. Use Info.{Defs,Uses,Implicits} for the results of name resolution.
名称解析将程序中的每个标识符 (ast.Ident) 映射到它表示的语言对象 (Object)。使用 Info.{Defs,Uses,Implicits} 获取名称解析的结果。
Constant folding computes the exact constant value (constant.Value) for every expression (ast.Expr) that is a compile-time constant. Use Info.Types[expr].Value for the results of constant folding.
常量折叠为每个编译时常量表达式 (ast.Expr) 计算精确的常量值 (constant.Value)。使用 Info.Types[expr].Value 获取常量折叠的结果。
Type inference computes the type (Type) of every expression (ast.Expr) and checks for compliance with the language specification. Use Info.Types[expr].Type for the results of type inference.
类型推断计算每个表达式 (ast.Expr) 的类型 (Type),并检查是否符合语言规范。使用 Info.Types[expr].Type 获取类型推断的结果。
For a tutorial, see https://golang.org/s/types-tutorial.
有关教程,请参阅 https://golang.org/s/types-tutorial。
常量
This section is empty.
变量
|
|
Typ contains the predeclared *Basic types indexed by their corresponding BasicKind.
Typ 包含由其对应的 BasicKind 索引的前置声明的 *Basic 类型。
The *Basic type for Typ[Byte] will have the name “uint8”. Use Universe.Lookup(“byte”).Type() to obtain the specific alias basic type named “byte” (and analogous for “rune”).
Typ[Byte] 的基本类型将命名为“uint8”。使用 Universe.Lookup(“byte”).Type() 来获取名为“byte”的特定别名基本类型(对于“rune”也是类似的)。
函数
func AssertableTo
|
|
AssertableTo reports whether a value of type V can be asserted to have type T.
AssertableTo 报告类型 V 的值是否可以断言为类型 T。
The behavior of AssertableTo is unspecified in three cases:
AssertableTo 的行为在三种情况下未指定:
- if T is Typ[Invalid] 如果 T 是 Typ[Invalid]
- if V is a generalized interface; i.e., an interface that may only be used as a type constraint in Go code 如果 V 是泛型接口;即,只能在 Go 代码中用作类型约束的接口
- if T is an uninstantiated generic type 如果 T 是未实例化的泛型类型
func AssignableTo
|
|
AssignableTo reports whether a value of type V is assignable to a variable of type T.
AssignableTo 报告类型 V 的值是否可赋值给类型 T 的变量。
The behavior of AssignableTo is unspecified if V or T is Typ[Invalid] or an uninstantiated generic type.
如果 V 或 T 是 Typ[Invalid] 或未实例化的泛型类型,则 AssignableTo 的行为未指定。
func CheckExpr <- go1.13
|
|
CheckExpr type checks the expression expr as if it had appeared at position pos of package pkg. Type information about the expression is recorded in info. The expression may be an identifier denoting an uninstantiated generic function or type.
CheckExpr 类型检查表达式 expr,就好像它出现在包 pkg 的位置 pos。有关表达式的类型信息记录在 info 中。该表达式可以是表示未实例化泛型函数或类型的标识符。
If pkg == nil, the Universe scope is used and the provided position pos is ignored. If pkg != nil, and pos is invalid, the package scope is used. Otherwise, pos must belong to the package.
如果 pkg == nil,则使用 Universe 作用域,并忽略提供的位置 pos。如果 pkg != nil,并且 pos 无效,则使用包作用域。否则,pos 必须属于该包。
An error is returned if pos is not within the package or if the node cannot be type-checked.
如果 pos 不在包内或无法对节点进行类型检查,则会返回错误。
Note: Eval and CheckExpr should not be used instead of running Check to compute types and values, but in addition to Check, as these functions ignore the context in which an expression is used (e.g., an assignment). Thus, top-level untyped constants will return an untyped type rather then the respective context-specific type.
注意:Eval 和 CheckExpr 不应代替运行 Check 来计算类型和值,而应与 Check 一起使用,因为这些函数忽略表达式使用的上下文(例如,赋值)。因此,顶级无类型常量将返回无类型类型,而不是各自的特定于上下文的类型。
func Comparable
|
|
Comparable reports whether values of type T are comparable.
Comparable 报告类型 T 的值是否可比较。
func ConvertibleTo
|
|
ConvertibleTo reports whether a value of type V is convertible to a value of type T.
ConvertibleTo 报告类型 V 的值是否可转换为类型 T 的值。
The behavior of ConvertibleTo is unspecified if V or T is Typ[Invalid] or an uninstantiated generic type.
如果 V 或 T 是 Typ[Invalid] 或未实例化的泛型类型,则 ConvertibleTo 的行为未指定。
func DefPredeclaredTestFuncs
|
|
DefPredeclaredTestFuncs defines the assert and trace built-ins. These built-ins are intended for debugging and testing of this package only.
DefPredeclaredTestFuncs 定义 assert 和 trace 内置函数。这些内置函数仅用于调试和测试此软件包。
func ExprString
|
|
ExprString returns the (possibly shortened) string representation for x. Shortened representations are suitable for user interfaces but may not necessarily follow Go syntax.
ExprString 返回 x 的(可能已缩短的)字符串表示形式。缩短的表示形式适用于用户界面,但不一定遵循 Go 语法。
func Id
|
|
Id returns name if it is exported, otherwise it returns the name qualified with the package path.
如果导出,Id 返回名称,否则返回使用包路径限定的名称。
func Identical
|
|
Identical reports whether x and y are identical types. Receivers of Signature types are ignored.
Identical 报告 x 和 y 是否是相同的类型。Signature 类型的接收者被忽略。
func IdenticalIgnoreTags <- go1.8
|
|
IdenticalIgnoreTags reports whether x and y are identical types if tags are ignored. Receivers of Signature types are ignored.
IdenticalIgnoreTags 报告在忽略标签的情况下 x 和 y 是否为相同类型。Signature 类型的接收者被忽略。
func Implements
|
|
Implements reports whether type V implements interface T.
Implements 报告类型 V 是否实现接口 T。
The behavior of Implements is unspecified if V is Typ[Invalid] or an uninstantiated generic type.
如果 V 是 Typ[Invalid] 或未实例化的泛型类型,则 Implements 的行为未指定。
func IsInterface
|
|
IsInterface reports whether t is an interface type.
IsInterface 报告 t 是否为接口类型。
func ObjectString
|
|
ObjectString returns the string form of obj. The Qualifier controls the printing of package-level objects, and may be nil.
ObjectString 返回 obj 的字符串形式。Qualifier 控制包级对象的打印,可以为 nil。
func Satisfies <- go1.20
|
|
Satisfies reports whether type V satisfies the constraint T.
Satisfies 报告类型 V 是否满足约束 T。
The behavior of Satisfies is unspecified if V is Typ[Invalid] or an uninstantiated generic type.
如果 V 是 Typ[Invalid] 或未实例化的泛型类型,则 Satisfies 的行为未指定。
func SelectionString
|
|
SelectionString returns the string form of s. The Qualifier controls the printing of package-level objects, and may be nil.
SelectionString 返回 s 的字符串形式。Qualifier 控制包级对象的打印,可以为 nil。
Examples:
示例:
"field (T) f int"
"method (T) f(X) Y"
"method expr (T) f(X) Y"
func TypeString
|
|
TypeString returns the string representation of typ. The Qualifier controls the printing of package-level objects, and may be nil.
TypeString 返回 typ 的字符串表示形式。Qualifier 控制包级对象的打印,可以为 nil。
func WriteExpr
|
|
WriteExpr writes the (possibly shortened) string representation for x to buf. Shortened representations are suitable for user interfaces but may not necessarily follow Go syntax.
WriteExpr 将 x 的(可能缩短的)字符串表示形式写入 buf。缩短的表示形式适用于用户界面,但不一定遵循 Go 语法。
func WriteSignature
|
|
WriteSignature writes the representation of the signature sig to buf, without a leading “func” keyword. The Qualifier controls the printing of package-level objects, and may be nil.
WriteSignature 将签名 sig 的表示形式写入 buf,不带前导“func”关键字。Qualifier 控制包级对象的打印,可以为 nil。
func WriteType
|
|
WriteType writes the string representation of typ to buf. The Qualifier controls the printing of package-level objects, and may be nil.
WriteType 将 typ 的字符串表示形式写入 buf。Qualifier 控制包级对象的打印,可以为 nil。
类型
type ArgumentError <- go1.18
|
|
An ArgumentError holds an error associated with an argument index.
ArgumentError 持有一个与参数索引关联的错误。
(*ArgumentError) Error <- go1.18
|
|
(*ArgumentError) Unwrap <- go1.18
|
|
type Array
|
|
An Array represents an array type.
Array 表示一个数组类型。
func NewArray
|
|
NewArray returns a new array type for the given element type and length. A negative length indicates an unknown length.
NewArray 为给定的元素类型和长度返回一个新的数组类型。负长度表示未知长度。
(*Array) Elem
|
|
Elem returns element type of array a.
Elem 返回数组 a 的元素类型。
(*Array) Len
|
|
Len returns the length of array a. A negative result indicates an unknown length.
Len 返回数组 a 的长度。负结果表示未知长度。
(*Array) String
|
|
(*Array) Underlying
|
|
type Basic
|
|
A Basic represents a basic type.
基本表示基本类型。
(*Basic) Info
|
|
Info returns information about properties of basic type b.
Info 返回有关基本类型 b 的属性的信息。
(*Basic) Kind
|
|
Kind returns the kind of basic type b.
类型返回基本类型 b 的类型。
(*Basic) Name
|
|
Name returns the name of basic type b.
名称返回基本类型 b 的名称。
(*Basic) String
|
|
(*Basic) Underlying
|
|
type BasicInfo
|
|
BasicInfo is a set of flags describing properties of a basic type.
BasicInfo 是描述基本类型属性的一组标志。
|
|
Properties of basic types.
基本类型的属性。
type BasicKind
|
|
BasicKind describes the kind of basic type.
BasicKind 描述基本类型的种类。
|
|
type Builtin
|
|
A Builtin represents a built-in function. Builtins don’t have a valid type.
Builtin 表示内置函数。Builtin 没有有效类型。
(*Builtin) Exported
|
|
Exported reports whether the object is exported (starts with a capital letter). It doesn’t take into account whether the object is in a local (function) scope or not.
Exported 报告对象是否导出(以大写字母开头)。它不考虑对象是否在本地(函数)作用域中。
(*Builtin) Id
|
|
Id is a wrapper for Id(obj.Pkg(), obj.Name()).
Id 是 Id(obj.Pkg(), obj.Name()) 的包装器。
(*Builtin) Name
|
|
Name returns the object’s (package-local, unqualified) name.
Name 返回对象(包本地,不合格)的名称。
(*Builtin) Parent
|
|
Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.
Parent 返回声明对象的范围。对于方法和结构字段,结果为 nil。
(*Builtin) Pkg
|
|
Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.
Pkg 返回对象所属的包。对于 Universe 范围内的标签和对象,结果为 nil。
(*Builtin) Pos
|
|
Pos returns the declaration position of the object’s identifier.
Pos 返回对象标识符的声明位置。
(*Builtin) String
|
|
(*Builtin) Type
|
|
Type returns the object’s type.
Type 返回对象类型。
type Chan
|
|
A Chan represents a channel type.
A Chan 表示一个通道类型。
func NewChan
|
|
NewChan returns a new channel type for the given direction and element type.
NewChan 为给定的方向和元素类型返回一个新的通道类型。
(*Chan) Dir
|
|
Dir returns the direction of channel c.
Dir 返回通道 c 的方向。
(*Chan) Elem
|
|
Elem returns the element type of channel c.
Elem 返回通道 c 的元素类型。
(*Chan) String
|
|
(*Chan) Underlying
|
|
type ChanDir
|
|
A ChanDir value indicates a channel direction.
A ChanDir 值指示通道方向。
|
|
The direction of a channel is indicated by one of these constants.
通道的方向由以下常量之一指示。
type Checker
|
|
A Checker maintains the state of the type checker. It must be created with NewChecker.
检查器维护类型检查器的状态。它必须使用 NewChecker 创建。
func NewChecker
|
|
NewChecker returns a new Checker instance for a given package. Package files may be added incrementally via checker.Files.
NewChecker 为给定程序包返回一个新的 Checker 实例。程序包文件可以通过 checker.Files 逐步添加。
(*Checker) Files
|
|
Files checks the provided files as part of the checker’s package.
文件检查程序作为检查程序包的一部分检查提供的文件。
type Config
|
|
A Config specifies the configuration for type checking. The zero value for Config is a ready-to-use default configuration.
Config 指定类型检查的配置。Config 的零值是即用型默认配置。
(*Config) Check
|
|
Check type-checks a package and returns the resulting package object and the first error if any. Additionally, if info != nil, Check populates each of the non-nil maps in the Info struct.
Check 类型检查一个包并返回结果包对象和第一个错误(如果有)。此外,如果 info 不为 nil,Check 会填充 Info 结构中每个非 nil 的映射。
The package is marked as complete if no errors occurred, otherwise it is incomplete. See Config.Error for controlling behavior in the presence of errors.
如果未发生错误,则将包标记为已完成,否则为未完成。有关在存在错误时控制行为的信息,请参阅 Config.Error。
The package is specified by a list of *ast.Files and corresponding file set, and the package path the package is identified with. The clean path must not be empty or dot (".").
包由 *ast.Files 和相应的文件集的列表以及包的标识路径指定。干净的路径不能是空或点(“.”)。
type Const
|
|
A Const represents a declared constant.
Const 表示已声明的常量。
func NewConst
|
|
NewConst returns a new constant with value val. The remaining arguments set the attributes found with all Objects.
NewConst 返回一个值为 val 的新常量。其余参数设置所有对象中找到的属性。
(*Const) Exported
|
|
Exported reports whether the object is exported (starts with a capital letter). It doesn’t take into account whether the object is in a local (function) scope or not.
Exported 报告对象是否导出(以大写字母开头)。它不考虑对象是否在本地(函数)作用域中。
(*Const) Id
|
|
Id is a wrapper for Id(obj.Pkg(), obj.Name()).
Id 是 Id(obj.Pkg(), obj.Name()) 的包装器。
(*Const) Name
|
|
Name returns the object’s (package-local, unqualified) name.
Name 返回对象(包本地,不合格)的名称。
(*Const) Parent
|
|
Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.
Parent 返回声明对象的作用域。对于方法和结构字段,结果为 nil。
(*Const) Pkg
|
|
Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.
Pkg 返回对象所属的包。对于 Universe 范围内的标签和对象,结果为 nil。
(*Const) Pos
|
|
Pos returns the declaration position of the object’s identifier.
Pos 返回对象标识符的声明位置。
(*Const) String
|
|
(*Const) Type
|
|
Type returns the object’s type.
Type 返回对象类型。
(*Const) Val
|
|
Val returns the constant’s value.
Val 返回常量值。
type Context <- go1.18
|
|
A Context is an opaque type checking context. It may be used to share identical type instances across type-checked packages or calls to Instantiate. Contexts are safe for concurrent use.
上下文是不透明的类型检查上下文。它可用于在类型检查的包或对 Instantiate 的调用之间共享相同的类型实例。上下文可安全地用于并发使用。
The use of a shared context does not guarantee that identical instances are deduplicated in all cases.
使用共享上下文并不能保证在所有情况下都对相同的实例进行去重。
func NewContext <- go1.18
|
|
NewContext creates a new Context.
NewContext 创建一个新的 Context。
type Error
|
|
An Error describes a type-checking error; it implements the error interface. A “soft” error is an error that still permits a valid interpretation of a package (such as “unused variable”); “hard” errors may lead to unpredictable behavior if ignored.
错误描述了一个类型检查错误;它实现了错误接口。 “软”错误是一个仍然允许对包进行有效解释的错误(例如“未使用变量”);如果忽略“硬”错误,可能会导致不可预测的行为。
(Error) Error
|
|
Error returns an error string formatted as follows: filename:line:column: message
错误返回一个格式如下所示的错误字符串:文件名:行:列:消息
type Func
|
|
A Func represents a declared function, concrete method, or abstract (interface) method. Its Type() is always a *Signature. An abstract method may belong to many interfaces due to embedding.
Func 表示已声明的函数、具体方法或抽象(接口)方法。其 Type() 始终是 *Signature。抽象方法由于嵌入可能属于多个接口。
func MissingMethod
|
|
MissingMethod returns (nil, false) if V implements T, otherwise it returns a missing method required by T and whether it is missing or just has the wrong type.
如果 V 实现 T,则 MissingMethod 返回 (nil, false),否则它返回 T 所需的缺失方法,以及它是缺失还是仅类型错误。
For non-interface types V, or if static is set, V implements T if all methods of T are present in V. Otherwise (V is an interface and static is not set), MissingMethod only checks that methods of T which are also present in V have matching types (e.g., for a type assertion x.(T) where x is of interface type V).
对于非接口类型 V,或者如果设置了 static,则当 T 的所有方法都存在于 V 中时,V 实现 T。否则(V 是接口且未设置 static),MissingMethod 仅检查 T 中也存在于 V 中的方法是否具有匹配的类型(例如,对于类型断言 x.(T),其中 x 为接口类型 V)。
func NewFunc
|
|
NewFunc returns a new function with the given signature, representing the function’s type.
NewFunc 返回具有给定签名的函数的新函数,表示函数的类型。
(*Func) Exported
|
|
Exported reports whether the object is exported (starts with a capital letter). It doesn’t take into account whether the object is in a local (function) scope or not.
Exported 报告对象是否已导出(以大写字母开头)。它不考虑对象是否在本地(函数)范围内。
(*Func) FullName
|
|
FullName returns the package- or receiver-type-qualified name of function or method obj.
FullName 返回函数或方法 obj 的包或接收器类型限定名称。
(*Func) Id
|
|
Id is a wrapper for Id(obj.Pkg(), obj.Name()).
Id 是 Id(obj.Pkg(), obj.Name()) 的包装器。
(*Func) Name
|
|
Name returns the object’s (package-local, unqualified) name.
Name 返回对象(包本地,不合格)的名称。
(*Func) Origin <- go1.19
|
|
Origin returns the canonical Func for its receiver, i.e. the Func object recorded in Info.Defs.
Origin 返回其接收者的规范 Func,即 Info.Defs 中记录的 Func 对象。
For synthetic functions created during instantiation (such as methods on an instantiated Named type or interface methods that depend on type arguments), this will be the corresponding Func on the generic (uninstantiated) type. For all other Funcs Origin returns the receiver.
对于在实例化期间创建的合成函数(例如,实例化 Named 类型上的方法或依赖于类型参数的接口方法),这将是泛型(未实例化)类型上的相应 Func。对于所有其他 Func,Origin 返回接收者。
(*Func) Parent
|
|
Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.
Parent 返回声明对象的作用域。对于方法和结构字段,结果为 nil。
(*Func) Pkg
|
|
Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.
Pkg 返回对象所属的包。对于 Universe 范围内的标签和对象,结果为 nil。
(*Func) Pos
|
|
Pos returns the declaration position of the object’s identifier.
Pos 返回对象标识符的声明位置。
(*Func) Scope
|
|
Scope returns the scope of the function’s body block. The result is nil for imported or instantiated functions and methods (but there is also no mechanism to get to an instantiated function).
Scope 返回函数主体块的范围。对于导入或实例化的函数和方法,结果为 nil(但也没有机制可以访问实例化的函数)。
(*Func) String
|
|
(*Func) Type
|
|
Type returns the object’s type.
Type 返回对象类型。
type ImportMode <- go1.6
|
|
ImportMode is reserved for future use.
ImportMode 保留供将来使用。
type Importer
|
|
An Importer resolves import paths to Packages.
导入器将导入路径解析为包。
CAUTION: This interface does not support the import of locally vendored packages. See https://golang.org/s/go15vendor. If possible, external implementations should implement ImporterFrom.
注意:此接口不支持导入本地供应商包。请参阅 https://golang.org/s/go15vendor。如果可能,外部实现应实现 ImporterFrom。
type ImporterFrom <- go1.6
|
|
An ImporterFrom resolves import paths to packages; it supports vendoring per https://golang.org/s/go15vendor. Use go/importer to obtain an ImporterFrom implementation.
ImporterFrom 将导入路径解析为包;它支持 https://golang.org/s/go15vendor 中的供应商。使用 go/importer 获取 ImporterFrom 实现。
type Info
|
|
Info holds result type information for a type-checked package. Only the information for which a map is provided is collected. If the package has type errors, the collected information may be incomplete.
Info 为类型检查包保存结果类型信息。仅收集提供映射的信息。如果包有类型错误,则收集的信息可能不完整。
Example
ExampleInfo prints various facts recorded by the type checker in a types.Info struct: definitions of and references to each named object, and the type, value, and mode of every expression in the package.
ExampleInfo 在 types.Info 结构中打印类型检查器记录的各种事实:每个命名对象的定义和引用,以及包中每个表达式的类型、值和模式。
|
|
(*Info) ObjectOf
|
|
ObjectOf returns the object denoted by the specified id, or nil if not found.
ObjectOf 返回由指定 id 表示的对象,如果未找到,则返回 nil。
If id is an embedded struct field, ObjectOf returns the field (*Var) it defines, not the type (*TypeName) it uses.
如果 id 是嵌入式结构字段,ObjectOf 返回它定义的字段 (*Var),而不是它使用的类型 (*TypeName)。
Precondition: the Uses and Defs maps are populated.
前提条件:Uses 和 Defs 映射已填充。
(*Info) TypeOf
|
|
TypeOf returns the type of expression e, or nil if not found. Precondition: the Types, Uses and Defs maps are populated.
TypeOf 返回表达式 e 的类型,如果未找到,则返回 nil。先决条件:已填充 Types、Uses 和 Defs 映射。
type Initializer
|
|
An Initializer describes a package-level variable, or a list of variables in case of a multi-valued initialization expression, and the corresponding initialization expression.
初始化器描述了一个包级变量,或者在多值初始化表达式的情况下描述了一个变量列表,以及相应的初始化表达式。
(*Initializer) String
|
|
type Instance <- go1.18
|
|
Instance reports the type arguments and instantiated type for type and function instantiations. For type instantiations, Type will be of dynamic type *Named. For function instantiations, Type will be of dynamic type *Signature.
实例报告类型参数和类型和函数实例化的实例化类型。对于类型实例化,Type 将为动态类型 *Named。对于函数实例化,Type 将为动态类型 *Signature。
type Interface
|
|
An Interface represents an interface type.
接口表示一个接口类型。
func NewInterface <-DEPRECATED
|
|
NewInterface returns a new interface for the given methods and embedded types. NewInterface takes ownership of the provided methods and may modify their types by setting missing receivers.
NewInterface 为给定方法和嵌入式类型返回一个新接口。NewInterface 拥有所提供方法的所有权,并且可以通过设置缺失的接收器来修改其类型。
Deprecated: Use NewInterfaceType instead which allows arbitrary embedded types.
已弃用:改用 NewInterfaceType,它允许任意嵌入式类型。
func NewInterfaceType <- go1.11
|
|
NewInterfaceType returns a new interface for the given methods and embedded types. NewInterfaceType takes ownership of the provided methods and may modify their types by setting missing receivers.
NewInterfaceType 为给定方法和嵌入式类型返回一个新接口。NewInterfaceType 拥有所提供的这些方法的所有权,并且可以通过设置缺失的接收器来修改它们类型。
To avoid race conditions, the interface’s type set should be computed before concurrent use of the interface, by explicitly calling Complete.
为了避免竞争条件,接口的类型集应该在并发使用接口之前通过显式调用 Complete 来计算。
(*Interface) Complete
|
|
Complete computes the interface’s type set. It must be called by users of NewInterfaceType and NewInterface after the interface’s embedded types are fully defined and before using the interface type in any way other than to form other types. The interface must not contain duplicate methods or a panic occurs. Complete returns the receiver.
Complete 计算接口的类型集。在接口的嵌入式类型完全定义之后,并且在以形成其他类型以外的任何方式使用接口类型之前,NewInterfaceType 和 NewInterface 的用户必须调用它。接口中不得包含重复的方法,否则会发生 panic。Complete 返回接收器。
Interface types that have been completed are safe for concurrent use.
已完成的接口类型可安全地并发使用。
(*Interface) Embedded <-DEPRECATED
|
|
Embedded returns the i’th embedded defined (*Named) type of interface t for 0 <= i < t.NumEmbeddeds(). The result is nil if the i’th embedded type is not a defined type.
Embedded 返回接口 t 的第 i 个嵌入式已定义的 (*Named) 类型,其中 0 <= i < t.NumEmbeddeds()。如果第 i 个嵌入式类型不是已定义类型,则结果为 nil。
Deprecated: Use EmbeddedType which is not restricted to defined (*Named) types.
已弃用:使用 EmbeddedType,它不限于已定义的 (*Named) 类型。
(*Interface) EmbeddedType <- go1.11
|
|
EmbeddedType returns the i’th embedded type of interface t for 0 <= i < t.NumEmbeddeds().
EmbeddedType 返回接口 t 的第 i 个嵌入式类型,其中 0 <= i < t.NumEmbeddeds()。
(*Interface) Empty
|
|
Empty reports whether t is the empty interface.
Empty 报告 t 是否为空接口。
(*Interface) ExplicitMethod
|
|
ExplicitMethod returns the i’th explicitly declared method of interface t for 0 <= i < t.NumExplicitMethods(). The methods are ordered by their unique Id.
ExplicitMethod 返回接口 t 的第 i 个显式声明的方法,其中 0 <= i < t.NumExplicitMethods()。这些方法按其唯一 Id 排序。
(*Interface) IsComparable <- go1.18
|
|
IsComparable reports whether each type in interface t’s type set is comparable.
IsComparable 报告接口 t 的类型集中每个类型是否可比较。
(*Interface) IsImplicit <- go1.18
|
|
IsImplicit reports whether the interface t is a wrapper for a type set literal.
IsImplicit 报告接口 t 是否是类型集文字的包装器。
(*Interface) IsMethodSet <- go1.18
|
|
IsMethodSet reports whether the interface t is fully described by its method set.
IsMethodSet 报告接口 t 是否由其方法集完全描述。
(*Interface) MarkImplicit <- go1.18
|
|
MarkImplicit marks the interface t as implicit, meaning this interface corresponds to a constraint literal such as ~T or A|B without explicit interface embedding. MarkImplicit should be called before any concurrent use of implicit interfaces.
MarkImplicit 将接口 t 标记为隐式,这意味着此接口对应于约束文字,例如 ~T 或 A|B,而没有显式接口嵌入。应在隐式接口的任何并发使用之前调用 MarkImplicit。
(*Interface) Method
|
|
Method returns the i’th method of interface t for 0 <= i < t.NumMethods(). The methods are ordered by their unique Id.
方法返回接口 t 的第 i 个方法,其中 0 <= i < t.NumMethods()。这些方法按其唯一 Id 排序。
(*Interface) NumEmbeddeds
|
|
NumEmbeddeds returns the number of embedded types in interface t.
NumEmbeddeds 返回接口 t 中的嵌入式类型的数量。
(*Interface) NumExplicitMethods
|
|
NumExplicitMethods returns the number of explicitly declared methods of interface t.
NumExplicitMethods 返回接口 t 中显式声明的方法数。
(*Interface) NumMethods
|
|
NumMethods returns the total number of methods of interface t.
NumMethods 返回接口 t 的方法总数。
(*Interface) String
|
|
(*Interface) Underlying
|
|
type Label
|
|
A Label represents a declared label. Labels don’t have a type.
标签表示已声明的标签。标签没有类型。
func NewLabel
|
|
NewLabel returns a new label.
NewLabel 返回一个新标签。
(*Label) Exported
|
|
Exported reports whether the object is exported (starts with a capital letter). It doesn’t take into account whether the object is in a local (function) scope or not.
Exported 报告对象是否已导出(以大写字母开头)。它不考虑对象是否在本地(函数)范围内。
(*Label) Id
|
|
Id is a wrapper for Id(obj.Pkg(), obj.Name()).
Id 是 Id(obj.Pkg(), obj.Name()) 的包装器。
(*Label) Name
|
|
Name returns the object’s (package-local, unqualified) name.
Name 返回对象(包本地、不合格)的名称。
(*Label) Parent
|
|
Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.
Parent 返回声明对象的作用域。对于方法和结构字段,结果为 nil。
(*Label) Pkg
|
|
Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.
Pkg 返回对象所属的包。对于 Universe 范围内的标签和对象,结果为 nil。
(*Label) Pos
|
|
Pos returns the declaration position of the object’s identifier.
Pos 返回对象标识符的声明位置。
(*Label) String
|
|
(*Label) Type
|
|
Type returns the object’s type.
Type 返回对象类型。
type Map
|
|
A Map represents a map type.
Map 表示映射类型。
func NewMap
|
|
NewMap returns a new map for the given key and element types.
NewMap 返回一个具有给定键和元素类型的新映射。
(*Map) Elem
|
|
Elem returns the element type of map m.
Elem 返回映射 m 的元素类型。
(*Map) Key
|
|
Key returns the key type of map m.
Key 返回映射 m 的键类型。
(*Map) String
|
|
(*Map) Underlying
|
|
type MethodSet
|
|
A MethodSet is an ordered set of concrete or abstract (interface) methods; a method is a MethodVal selection, and they are ordered by ascending m.Obj().Id(). The zero value for a MethodSet is a ready-to-use empty method set. 重试 错误原因
Example
ExampleMethodSet prints the method sets of various types.
ExampleMethodSet 打印各种类型的函数集。
|
|
func NewMethodSet
|
|
NewMethodSet returns the method set for the given type T. It always returns a non-nil method set, even if it is empty.
NewMethodSet 返回给定类型 T 的方法集。即使方法集为空,它也始终返回一个非空方法集。
(*MethodSet) At
|
|
At returns the i’th method in s for 0 <= i < s.Len().
At 返回 s 中的第 i 个方法,其中 0 <= i < s.Len()。
(*MethodSet) Len
|
|
Len returns the number of methods in s.
Len 返回 s 中的方法数。
(*MethodSet) Lookup
|
|
Lookup returns the method with matching package and name, or nil if not found.
Lookup 返回具有匹配包和名称的方法,如果未找到,则返回 nil。
(*MethodSet) String
|
|
type Named
|
|
A Named represents a named (defined) type.
Named 表示一个命名(已定义)的类型。
func NewNamed
|
|
NewNamed returns a new named type for the given type name, underlying type, and associated methods. If the given type name obj doesn’t have a type yet, its type is set to the returned named type. The underlying type must not be a *Named.
NewNamed 为给定的类型名称、基础类型和关联方法返回一个新的命名类型。如果给定的类型名称 obj 还没有类型,则其类型将设置为返回的命名类型。基础类型不能是 *Named。
(*Named) AddMethod
|
|
AddMethod adds method m unless it is already in the method list. t must not have type arguments.
AddMethod 添加方法 m,除非它已在方法列表中。t 不得具有类型参数。
(*Named) Method
|
|
Method returns the i’th method of named type t for 0 <= i < t.NumMethods().
方法返回命名类型 t 的第 i 个方法,其中 0 <= i < t.NumMethods()。
For an ordinary or instantiated type t, the receiver base type of this method is the named type t. For an uninstantiated generic type t, each method receiver is instantiated with its receiver type parameters.
对于普通或实例化类型 t,此方法的接收器基类型是命名类型 t。对于未实例化的泛型类型 t,每个方法接收器都使用其接收器类型参数进行实例化。
(*Named) NumMethods
|
|
NumMethods returns the number of explicit methods defined for t.
NumMethods 返回为 t 定义的显式方法的数量。
(*Named) Obj
|
|
Obj returns the type name for the declaration defining the named type t. For instantiated types, this is same as the type name of the origin type.
Obj 返回定义命名类型 t 的声明的类型名称。对于实例化类型,这与原始类型的类型名称相同。
(*Named) Origin <- go1.18
|
|
Origin returns the generic type from which the named type t is instantiated. If t is not an instantiated type, the result is t.
Origin 返回命名类型 t 实例化的泛型类型。如果 t 不是实例化类型,则结果为 t。
(*Named) SetTypeParams <- go1.18
|
|
SetTypeParams sets the type parameters of the named type t. t must not have type arguments.
SetTypeParams 设置命名类型 t 的类型参数。t 不得具有类型参数。
(*Named) SetUnderlying
|
|
SetUnderlying sets the underlying type and marks t as complete. t must not have type arguments.
SetUnderlying 设置基础类型并将 t 标记为已完成。t 不得具有类型参数。
(*Named) String
|
|
(*Named) TypeArgs <- go1.18
|
|
TypeArgs returns the type arguments used to instantiate the named type t.
TypeArgs 返回用于实例化命名类型 t 的类型参数。
(*Named) TypeParams <- go1.18
|
|
TypeParams returns the type parameters of the named type t, or nil. The result is non-nil for an (originally) generic type even if it is instantiated.
TypeParams 返回命名类型 t 的类型参数,或 nil。即使实例化,结果对于(最初的)泛型类型也是非 nil。
(*Named) Underlying
|
|
type Nil
|
|
Nil represents the predeclared value nil.
Nil 表示预声明值 nil。
(*Nil) Exported
|
|
Exported reports whether the object is exported (starts with a capital letter). It doesn’t take into account whether the object is in a local (function) scope or not.
导出报告对象是否导出(以大写字母开头)。它不考虑对象是否在本地(函数)范围内。
(*Nil) Id
|
|
Id is a wrapper for Id(obj.Pkg(), obj.Name()).
Id 是 Id(obj.Pkg(), obj.Name()) 的包装器。
(*Nil) Name
|
|
Name returns the object’s (package-local, unqualified) name.
Name 返回对象(包本地、不合格)的名称。
(*Nil) Parent
|
|
Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.
Parent 返回声明对象的作用域。对于方法和结构字段,结果为 nil。
(*Nil) Pkg
|
|
Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.
Pkg 返回对象所属的包。对于 Universe 范围内的标签和对象,结果为 nil。
(*Nil) Pos
|
|
Pos returns the declaration position of the object’s identifier.
Pos 返回对象标识符的声明位置。
(*Nil) String
|
|
(*Nil) Type
|
|
Type returns the object’s type.
Type 返回对象类型。
type Object
|
|
An Object describes a named language entity such as a package, constant, type, variable, function (incl. methods), or label. All objects implement the Object interface.
对象描述一个命名的语言实体,例如包、常量、类型、变量、函数(包括方法)或标签。所有对象都实现 Object 接口。
func LookupFieldOrMethod
|
|
LookupFieldOrMethod looks up a field or method with given package and name in T and returns the corresponding *Var or *Func, an index sequence, and a bool indicating if there were any pointer indirections on the path to the field or method. If addressable is set, T is the type of an addressable variable (only matters for method lookups). T must not be nil.
LookupFieldOrMethod 查找 T 中具有给定包和名称的字段或方法,并返回相应的 *Var 或 *Func、索引序列和一个布尔值,指示在字段或方法的路径上是否有任何指针间接。如果设置了可寻址,则 T 是可寻址变量的类型(仅对方法查找很重要)。T 不能为 nil。
The last index entry is the field or method index in the (possibly embedded) type where the entry was found, either:
最后一个索引条目是找到条目的(可能嵌入的)类型中的字段或方法索引,可能是:
- the list of declared methods of a named type; or 已命名类型的声明方法列表;或
- the list of all methods (method set) of an interface type; or 接口类型的全部方法列表(方法集);或
- the list of fields of a struct type. 结构类型字段列表。
The earlier index entries are the indices of the embedded struct fields traversed to get to the found entry, starting at depth 0.
较早的索引条目是从深度 0 开始,遍历以获取找到的条目而穿越的嵌入式结构字段的索引。
If no entry is found, a nil object is returned. In this case, the returned index and indirect values have the following meaning:
如果未找到条目,则返回 nil 对象。在这种情况下,返回的索引和间接值具有以下含义:
- If index != nil, the index sequence points to an ambiguous entry (the same name appeared more than once at the same embedding level). 如果索引!= nil,则索引序列指向一个不明确的条目(同一名称在同一嵌入级别出现多次)。
- If indirect is set, a method with a pointer receiver type was found but there was no pointer on the path from the actual receiver type to the method’s formal receiver base type, nor was the receiver addressable. 如果设置了 indirect,则找到一个具有指针接收器类型的方法,但从实际接收器类型到方法的正式接收器基本类型没有指针,接收器也不是可寻址的。
type Package
|
|
A Package describes a Go package.
Package 描述了一个 Go 包。
|
|
The Unsafe package is the package returned by an importer for the import path “unsafe”.
Unsafe 包是导入程序为导入路径“unsafe”返回的包。
func NewPackage
|
|
NewPackage returns a new Package for the given package path and name. The package is not complete and contains no explicit imports.
NewPackage 为给定的包路径和名称返回一个新包。该包不完整,不包含任何显式导入。
(*Package) Complete
|
|
A package is complete if its scope contains (at least) all exported objects; otherwise it is incomplete.
如果包的范围包含(至少)所有导出的对象,则该包是完整的;否则,该包是不完整的。
(*Package) GoVersion <-go1.21.0
|
|
GoVersion returns the minimum Go version required by this package. If the minimum version is unknown, GoVersion returns the empty string. Individual source files may specify a different minimum Go version, as reported in the go/ast.File.GoVersion field.
GoVersion 返回此包所需的最低 Go 版本。如果最低版本未知,GoVersion 返回空字符串。各个源文件可能会指定不同的最低 Go 版本,如 go/ast.File.GoVersion 字段中所述。
(*Package) Imports
|
|
Imports returns the list of packages directly imported by pkg; the list is in source order.
Imports 返回 pkg 直接导入的包列表;该列表按源顺序排列。
If pkg was loaded from export data, Imports includes packages that provide package-level objects referenced by pkg. This may be more or less than the set of packages directly imported by pkg’s source code.
如果 pkg 是从导出数据加载的,则 Imports 包括提供 pkg 引用的包级对象的包。这可能多于或少于 pkg 的源代码直接导入的包集。
If pkg uses cgo and the FakeImportC configuration option was enabled, the imports list may contain a fake “C” package.
如果 pkg 使用 cgo 并且启用了 FakeImportC 配置选项,则导入列表可能包含一个假的“C”包。
(*Package) MarkComplete
|
|
MarkComplete marks a package as complete.
MarkComplete 将一个包标记为已完成。
(*Package) Name
|
|
Name returns the package name.
Name 返回包名称。
(*Package) Path
|
|
Path returns the package path.
Path 返回包路径。
(*Package) Scope
|
|
Scope returns the (complete or incomplete) package scope holding the objects declared at package level (TypeNames, Consts, Vars, and Funcs). For a nil pkg receiver, Scope returns the Universe scope.
Scope 返回包含在包级别声明的对象(TypeName、Consts、Vars 和 Funcs)的(完整或不完整)包范围。对于 nil pkg 接收器,Scope 返回 Universe 范围。
(*Package) SetImports
|
|
SetImports sets the list of explicitly imported packages to list. It is the caller’s responsibility to make sure list elements are unique.
SetImports 将显式导入的包列表设置为 list。由调用者负责确保列表元素是唯一的。
(*Package) SetName <- go1.6
|
|
SetName sets the package name.
SetName 设置包名称。
(*Package) String
|
|
type PkgName
|
|
A PkgName represents an imported Go package. PkgNames don’t have a type.
PkgName 表示导入的 Go 包。PkgName 没有类型。
func NewPkgName
|
|
NewPkgName returns a new PkgName object representing an imported package. The remaining arguments set the attributes found with all Objects.
NewPkgName 返回一个表示导入包的新 PkgName 对象。其余参数设置在所有对象中找到的属性。
(*PkgName) Exported
|
|
Exported reports whether the object is exported (starts with a capital letter). It doesn’t take into account whether the object is in a local (function) scope or not.
Exported 报告对象是否导出(以大写字母开头)。它不考虑对象是否在本地(函数)范围内。
(*PkgName) Id
|
|
Id is a wrapper for Id(obj.Pkg(), obj.Name()).
Id 是 Id(obj.Pkg(), obj.Name()) 的包装器。
(*PkgName) Imported
|
|
Imported returns the package that was imported. It is distinct from Pkg(), which is the package containing the import statement.
Imported 返回导入的包。它不同于 Pkg(),后者是包含 import 语句的包。
(*PkgName) Name
|
|
Name returns the object’s (package-local, unqualified) name.
Name 返回对象(包本地、不合格)的名称。
(*PkgName) Parent
|
|
Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.
Parent 返回声明对象的作用域。对于方法和结构字段,结果为 nil。
(*PkgName) Pkg
|
|
Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.
Pkg 返回对象所属的包。对于 Universe 范围内的标签和对象,结果为 nil。
(*PkgName) Pos
|
|
Pos returns the declaration position of the object’s identifier.
Pos 返回对象标识符的声明位置。
(*PkgName) String
|
|
(*PkgName) Type
|
|
Type returns the object’s type.
Type 返回对象类型。
type Pointer
|
|
A Pointer represents a pointer type.
指针表示指针类型。
func NewPointer
|
|
NewPointer returns a new pointer type for the given element (base) type.
NewPointer 返回给定元素(基本)类型的新的指针类型。
(*Pointer) Elem
|
|
Elem returns the element type for the given pointer p.
Elem 返回给定指针 p 的元素类型。
(*Pointer) String
|
|
(*Pointer) Underlying
|
|
type Qualifier
|
|
A Qualifier controls how named package-level objects are printed in calls to TypeString, ObjectString, and SelectionString.
限定符控制在对 TypeString、ObjectString 和 SelectionString 调用时如何打印命名的包级对象。
These three formatting routines call the Qualifier for each package-level object O, and if the Qualifier returns a non-empty string p, the object is printed in the form p.O. If it returns an empty string, only the object name O is printed.
这三个格式化例程为每个包级对象 O 调用限定符,如果限定符返回非空字符串 p,则对象以 p.O 的形式打印。如果它返回空字符串,则只打印对象名称 O。
Using a nil Qualifier is equivalent to using (*Package).Path: the object is qualified by the import path, e.g., “encoding/json.Marshal”.
使用 nil 限定符等同于使用 (*Package).Path:对象由导入路径限定,例如,“encoding/json.Marshal”。
func RelativeTo
|
|
RelativeTo returns a Qualifier that fully qualifies members of all packages other than pkg.
RelativeTo 返回一个限定符,该限定符完全限定除 pkg 之外的所有包的成员。
type Scope
|
|
A Scope maintains a set of objects and links to its containing (parent) and contained (children) scopes. Objects may be inserted and looked up by name. The zero value for Scope is a ready-to-use empty scope.
作用域维护一组对象及其包含的(父)和包含的(子)作用域的链接。可以按名称插入和查找对象。Scope 的零值是一个可立即使用的空作用域。
Example
ExampleScope prints the tree of Scopes of a package created from a set of parsed files.
ExampleScope 打印由一组已解析文件创建的包的范围树。
|
|
The Universe scope contains all predeclared objects of Go. It is the outermost scope of any chain of nested scopes.
Universe 范围包含 Go 的所有预声明对象。它是任何嵌套范围链的最外层范围。
func NewScope
|
|
NewScope returns a new, empty scope contained in the given parent scope, if any. The comment is for debugging only.
NewScope 返回一个新的空范围,该范围包含在给定的父范围中(如果有)。注释仅用于调试。
(*Scope) Child
|
|
Child returns the i’th child scope for 0 <= i < NumChildren().
Child 返回 0 <= i < NumChildren() 的第 i 个子范围。
(*Scope) Contains
|
|
Contains reports whether pos is within the scope’s extent. The result is guaranteed to be valid only if the type-checked AST has complete position information.
Contains 报告 pos 是否在范围的范围内。仅当类型检查的 AST 具有完整的位置信息时,才能保证结果有效。
(*Scope) End
|
|
(*Scope) Innermost
|
|
Innermost returns the innermost (child) scope containing pos. If pos is not within any scope, the result is nil. The result is also nil for the Universe scope. The result is guaranteed to be valid only if the type-checked AST has complete position information.
Innermost 返回包含 pos 的最内部(子)作用域。如果 pos 不在任何作用域内,则结果为 nil。对于 Universe 作用域,结果也为 nil。仅当经过类型检查的 AST 具有完整的位置信息时,才能保证结果有效。
(*Scope) Insert
|
|
Insert attempts to insert an object obj into scope s. If s already contains an alternative object alt with the same name, Insert leaves s unchanged and returns alt. Otherwise it inserts obj, sets the object’s parent scope if not already set, and returns nil.
Insert 尝试将对象 obj 插入范围 s。如果 s 已包含具有相同名称的备用对象 alt,则 Insert 使 s 保持不变并返回 alt。否则,它会插入 obj,设置对象的父范围(如果尚未设置),并返回 nil。
(*Scope) Len
|
|
Len returns the number of scope elements.
Len 返回作用域元素的数量。
(*Scope) Lookup
|
|
Lookup returns the object in scope s with the given name if such an object exists; otherwise the result is nil.
Lookup 返回作用域 s 中具有给定名称的对象(如果存在这样的对象);否则结果为 nil。
(*Scope) LookupParent
|
|
LookupParent follows the parent chain of scopes starting with s until it finds a scope where Lookup(name) returns a non-nil object, and then returns that scope and object. If a valid position pos is provided, only objects that were declared at or before pos are considered. If no such scope and object exists, the result is (nil, nil).
LookupParent 遵循从 s 开始的范围父链,直到找到一个范围,其中 Lookup(name) 返回一个非空对象,然后返回该范围和对象。如果提供了有效位置 pos,则只考虑在 pos 处或之前声明的对象。如果不存在这样的范围和对象,则结果为 (nil, nil)。
Note that obj.Parent() may be different from the returned scope if the object was inserted into the scope and already had a parent at that time (see Insert). This can only happen for dot-imported objects whose scope is the scope of the package that exported them.
请注意,如果对象被插入到该范围中并且当时已经有一个父对象,则 obj.Parent() 可能与返回的范围不同(请参阅 Insert)。这仅适用于点导入的对象,其范围是导出它们的包的范围。
(*Scope) Names
|
|
Names returns the scope’s element names in sorted order.
Names 返回按排序顺序排列的范围的元素名称。
(*Scope) NumChildren
|
|
NumChildren returns the number of scopes nested in s.
NumChildren 返回 s 中嵌套的范围数。
(*Scope) Parent
|
|
Parent returns the scope’s containing (parent) scope.
Parent 返回范围的包含(父)范围。
(*Scope) Pos
|
|
Pos and End describe the scope’s source code extent [pos, end). The results are guaranteed to be valid only if the type-checked AST has complete position information. The extent is undefined for Universe and package scopes.
Pos 和 End 描述范围的源代码范围 [pos, end)。仅当经过类型检查的 AST 具有完整的位置信息时,才能保证结果有效。Universe 和包范围的范围是未定义的。
(*Scope) String
|
|
String returns a string representation of the scope, for debugging.
String 返回一个字符串表示形式的范围,用于调试。
(*Scope) WriteTo
|
|
WriteTo writes a string representation of the scope to w, with the scope elements sorted by name. The level of indentation is controlled by n >= 0, with n == 0 for no indentation. If recurse is set, it also writes nested (children) scopes.
WriteTo 将作用域的字符串表示形式写入 w,作用域元素按名称排序。缩进级别由 n >= 0 控制,n == 0 表示不缩进。如果设置了 recurse,它还会写入嵌套(子)作用域。
type Selection
|
|
A Selection describes a selector expression x.f. For the declarations:
选择描述选择器表达式 x.f。对于声明:
|
|
the following relations exist:
存在以下关系:
Selector Kind Recv Obj Type Index Indirect
p.x FieldVal T x int {0} true
p.m MethodVal *T m func() {1, 0} true
T.m MethodExpr T m func(T) {1, 0} false
(*Selection) Index
|
|
Index describes the path from x to f in x.f. The last index entry is the field or method index of the type declaring f; either:
索引描述了 x.f 中从 x 到 f 的路径。最后一个索引条目是声明 f 的类型的字段或方法索引;或者:
- the list of declared methods of a named type; or 已命名类型的已声明方法列表;或
- the list of methods of an interface type; or 接口类型的列表;或
- the list of fields of a struct type. 结构类型的字段列表。
The earlier index entries are the indices of the embedded fields implicitly traversed to get from (the type of) x to f, starting at embedding depth 0.
较早的索引条目是从(x 的类型)到 f 的隐式遍历的嵌入字段的索引,从嵌入深度 0 开始。
(*Selection) Indirect
|
|
Indirect reports whether any pointer indirection was required to get from x to f in x.f.
间接报告是否需要任何指针间接才能从 x.f 中的 x 获取 f。
(*Selection) Kind
|
|
Kind returns the selection kind.
Kind 返回选择类型。
(*Selection) Obj
|
|
Obj returns the object denoted by x.f; a *Var for a field selection, and a *Func in all other cases.
Obj 返回 x.f 表示的对象;对于字段选择,返回 *Var,在所有其他情况下返回 *Func。
(*Selection) Recv
|
|
Recv returns the type of x in x.f.
Recv 返回 x.f 中 x 的类型。
(*Selection) String
|
|
(*Selection) Type
|
|
Type returns the type of x.f, which may be different from the type of f. See Selection for more information.
Type 返回 x.f 的类型,该类型可能与 f 的类型不同。有关更多信息,请参阅 Selection。
type SelectionKind
|
|
SelectionKind describes the kind of a selector expression x.f (excluding qualified identifiers).
SelectionKind 描述选择器表达式 x.f 的类型(不包括限定标识符)。
|
|
type Signature
|
|
A Signature represents a (non-builtin) function or method type. The receiver is ignored when comparing signatures for identity.
签名表示(非内置)函数或方法类型。在比较签名以确定标识时,忽略接收器。
func NewSignature <-DEPRECATED
|
|
NewSignature returns a new function type for the given receiver, parameters, and results, either of which may be nil. If variadic is set, the function is variadic, it must have at least one parameter, and the last parameter must be of unnamed slice type. 重试 错误原因
Deprecated: Use NewSignatureType instead which allows for type parameters. 重试 错误原因
func NewSignatureType <- go1.18
|
|
NewSignatureType creates a new function type for the given receiver, receiver type parameters, type parameters, parameters, and results. If variadic is set, params must hold at least one parameter and the last parameter’s core type must be of unnamed slice or bytestring type. If recv is non-nil, typeParams must be empty. If recvTypeParams is non-empty, recv must be non-nil.
NewSignatureType 为给定的接收器、接收器类型参数、类型参数、参数和结果创建一个新的函数类型。如果设置了 variadic,则 params 必须至少包含一个参数,并且最后一个参数的核心类型必须是未命名切片或字节串类型。如果 recv 为非 nil,则 typeParams 必须为空。如果 recvTypeParams 为非空,则 recv 必须为非 nil。
(*Signature) Params
|
|
Params returns the parameters of signature s, or nil.
Params 返回签名 s 的参数,或 nil。
(*Signature) Recv
|
|
Recv returns the receiver of signature s (if a method), or nil if a function. It is ignored when comparing signatures for identity.
Recv 返回签名 s 的接收者(如果是方法)或 nil(如果是函数)。在比较签名以确定标识时,它会被忽略。
For an abstract method, Recv returns the enclosing interface either as a *Named or an *Interface. Due to embedding, an interface may contain methods whose receiver type is a different interface.
对于抽象方法,Recv 返回封闭接口,作为 *Named 或 *Interface。由于嵌入,接口可能包含其接收者类型是不同接口的方法。
(*Signature) RecvTypeParams <- go1.18
|
|
RecvTypeParams returns the receiver type parameters of signature s, or nil.
RecvTypeParams 返回签名 s 的接收器类型参数,或 nil。
(*Signature) Results
|
|
Results returns the results of signature s, or nil.
Results 返回签名 s 的结果,或 nil。
(*Signature) String
|
|
(*Signature) TypeParams <- go1.18
|
|
TypeParams returns the type parameters of signature s, or nil.
TypeParams 返回签名 s 的类型参数,或 nil。
(*Signature) Underlying
|
|
(*Signature) Variadic
|
|
Variadic reports whether the signature s is variadic.
Variadic 报告签名 s 是否是变参的。
type Sizes
|
|
Sizes defines the sizing functions for package unsafe.
Sizes 定义包 unsafe 的大小调整函数。
func SizesFor <- go1.9
|
|
SizesFor returns the Sizes used by a compiler for an architecture. The result is nil if a compiler/architecture pair is not known.
SizesFor 返回编译器针对某个架构使用的 Sizes。如果编译器/架构对未知,则结果为 nil。
Supported architectures for compiler “gc”: “386”, “amd64”, “amd64p32”, “arm”, “arm64”, “loong64”, “mips”, “mipsle”, “mips64”, “mips64le”, “ppc64”, “ppc64le”, “riscv64”, “s390x”, “sparc64”, “wasm”.
编译器“gc”支持的架构:“386”、“amd64”、“amd64p32”、“arm”、“arm64”、“loong64”、“mips”、“mipsle”、“mips64”、“mips64le”、“ppc64”、“ppc64le”、“riscv64”、“s390x”、“sparc64”、“wasm”。
type Slice
|
|
A Slice represents a slice type.
A Slice 表示一个切片类型。
func NewSlice
|
|
NewSlice returns a new slice type for the given element type.
NewSlice 返回给定元素类型的新切片类型。
(*Slice) Elem
|
|
Elem returns the element type of slice s. 重试 错误原因
(*Slice) String
|
|
(*Slice) Underlying
|
|
type StdSizes
|
|
StdSizes is a convenience type for creating commonly used Sizes. It makes the following simplifying assumptions:
StdSizes 是一种用于创建常用大小的便捷类型。它做出了以下简化假设:
- The size of explicitly sized basic types (int16, etc.) is the specified size. 显式大小的基本类型(int16 等)的大小是指定的大小。
- The size of strings and interfaces is 2WordSize. 字符串和接口的大小是 2WordSize。
- The size of slices is 3WordSize. 切片的大小是 3WordSize。
- The size of an array of n elements corresponds to the size of a struct of n consecutive fields of the array’s element type. 包含 n 个元素的数组的大小对应于 n 个连续字段的结构的大小,这些字段属于数组的元素类型。
- The size of a struct is the offset of the last field plus that field’s size. As with all element types, if the struct is used in an array its size must first be aligned to a multiple of the struct’s alignment. 结构的大小是最后一个字段的偏移量加上该字段的大小。与所有元素类型一样,如果结构用于数组,则必须先将其大小调整为结构对齐方式的倍数。
- All other types have size WordSize. 所有其他类型的大小为 WordSize。
- Arrays and structs are aligned per spec definition; all other types are naturally aligned with a maximum alignment MaxAlign. 数组和结构按照规范定义对齐;所有其他类型都以最大对齐方式 MaxAlign 自然对齐。
*StdSizes
implements Sizes.
*StdSizes
实现 Sizes。
(*StdSizes) Alignof
|
|
(*StdSizes) Offsetsof
|
|
(*StdSizes) Sizeof
|
|
type Struct
|
|
A Struct represents a struct type.
结构体表示一个结构体类型。
func NewStruct
|
|
NewStruct returns a new struct with the given fields and corresponding field tags. If a field with index i has a tag, tags[i] must be that tag, but len(tags) may be only as long as required to hold the tag with the largest index i. Consequently, if no field has a tag, tags may be nil.
NewStruct 返回具有给定字段和相应字段标记的新结构。如果索引为 i 的字段具有标记,则 tags[i] 必须是该标记,但 len(tags) 可能仅与容纳具有最大索引 i 的标记所需的一样长。因此,如果没有字段具有标记,则 tags 可能为 nil。
(*Struct) Field
|
|
Field returns the i’th field for 0 <= i < NumFields(). 重试 错误原因
(*Struct) NumFields
|
|
NumFields returns the number of fields in the struct (including blank and embedded fields).
NumFields 返回结构中的字段数(包括空白字段和嵌入字段)。
(*Struct) String
|
|
(*Struct) Tag
|
|
Tag returns the i’th field tag for 0 <= i < NumFields().
标签返回 0 <= i < NumFields() 的第 i 个字段标签。
(*Struct) Underlying
|
|
type Term <- go1.18
|
|
A Term represents a term in a Union.
术语表示联合中的一个术语。
func NewTerm <- go1.18
|
|
NewTerm returns a new union term.
NewTerm 返回一个新的联合项。
(*Term) String <- go1.18
|
|
(*Term) Tilde <- go1.18
|
|
(*Term) Type <- go1.18
|
|
type Tuple
|
|
A Tuple represents an ordered list of variables; a nil *Tuple is a valid (empty) tuple. Tuples are used as components of signatures and to represent the type of multiple assignments; they are not first class types of Go.
元组表示一个有序变量列表;nil *Tuple 是一个有效的(空)元组。元组用作签名组件和表示多个赋值的类型;它们不是 Go 的一等类型。
func NewTuple
|
|
NewTuple returns a new tuple for the given variables.
NewTuple 返回给定变量的新元组。
(*Tuple) At
|
|
At returns the i’th variable of tuple t.
At 返回元组 t 的第 i 个变量。
(*Tuple) Len
|
|
Len returns the number variables of tuple t.
Len 返回元组 t 的变量数。
(*Tuple) String
|
|
(*Tuple) Underlying
|
|
type Type
|
|
A Type represents a type of Go. All types implement the Type interface.
类型表示 Go 的一种类型。所有类型都实现了 Type 接口。
func Default <- go1.8
|
|
Default returns the default “typed” type for an “untyped” type; it returns the incoming type for all other types. The default type for untyped nil is untyped nil.
Default 返回“无类型”类型的默认“类型化”类型;它返回所有其他类型的传入类型。无类型 nil 的默认类型是无类型 nil。
func Instantiate <- go1.18
|
|
Instantiate instantiates the type orig with the given type arguments targs. orig must be a *Named
or a *Signature
type. If there is no error, the resulting Type is an instantiated type of the same kind (either a *Named
or a *Signature
). Methods attached to a *Named
type are also instantiated, and associated with a new *Func that has the same position as the original method, but nil function scope.
Instantiate 用给定的类型参数 targs 实例化类型 orig。orig 必须是 *Named
或 *Signature
类型。如果没有错误,则结果类型是相同类型的实例化类型(*Named
或 *Signature
)。附加到 *Named
类型的函数也会被实例化,并与具有与原始函数相同位置的新 *Func
相关联,但函数范围为 nil。
If ctxt is non-nil, it may be used to de-duplicate the instance against previous instances with the same identity. As a special case, generic *Signature origin types are only considered identical if they are pointer equivalent, so that instantiating distinct (but possibly identical) signatures will yield different instances. The use of a shared context does not guarantee that identical instances are deduplicated in all cases.
如果 ctxt 为非 nil,则可以使用它来根据具有相同标识的先前实例对实例进行去重。作为特例,仅当通用 *Signature
原始类型指针等价时,才认为它们是相同的,因此实例化不同的(但可能相同的)签名将产生不同的实例。使用共享上下文并不能保证在所有情况下都对相同的实例进行去重。
If validate is set, Instantiate verifies that the number of type arguments and parameters match, and that the type arguments satisfy their corresponding type constraints. If verification fails, the resulting error may wrap an *ArgumentError indicating which type argument did not satisfy its corresponding type parameter constraint, and why.
如果设置了 validate,则 Instantiate 会验证类型参数和参数的数量是否匹配,以及类型参数是否满足其对应的类型约束。如果验证失败,则结果错误可能会包装一个 *ArgumentError,指示哪个类型参数不满足其对应的类型参数约束,以及原因。
If validate is not set, Instantiate does not verify the type argument count or whether the type arguments satisfy their constraints. Instantiate is guaranteed to not return an error, but may panic. Specifically, for *Signature types, Instantiate will panic immediately if the type argument count is incorrect; for *Named types, a panic may occur later inside the *Named API.
如果未设置验证,Instantiate 不会验证类型参数计数或类型参数是否满足其约束。Instantiate 保证不会返回错误,但可能会引发 panic。具体来说,对于 *Signature 类型,如果类型参数计数不正确,Instantiate 会立即引发 panic;对于 *Named 类型,panic 可能会稍后在 *Named API 内部发生。
type TypeAndValue
|
|
TypeAndValue reports the type and value (for constants) of the corresponding expression.
TypeAndValue 报告相应表达式的类型和值(对于常量)。
func Eval
|
|
Eval returns the type and, if constant, the value for the expression expr, evaluated at position pos of package pkg, which must have been derived from type-checking an AST with complete position information relative to the provided file set.
Eval 返回类型,如果常量,则返回在包 pkg 的位置 pos 处计算的表达式 expr 的值,该包必须是从相对于所提供的文件集的完整位置信息进行类型检查的 AST 派生的。
The meaning of the parameters fset, pkg, and pos is the same as in CheckExpr. An error is returned if expr cannot be parsed successfully, or the resulting expr AST cannot be type-checked.
fset、pkg 和 pos 参数的含义与 CheckExpr 中的相同。如果无法成功解析 expr,或者无法对生成的 expr AST 进行类型检查,则会返回错误。
(TypeAndValue) Addressable
|
|
Addressable reports whether the corresponding expression is addressable (https://golang.org/ref/spec#Address_operators).
Addressable 报告相应的表达式是否可寻址 ( https://golang.org/ref/spec#Address_operators)。
(TypeAndValue) Assignable
|
|
Assignable reports whether the corresponding expression is assignable to (provided a value of the right type).
Assignable 报告相应的表达式是否可赋值给(提供一个正确类型的数值)。
(TypeAndValue) HasOk
|
|
HasOk reports whether the corresponding expression may be used on the rhs of a comma-ok assignment.
HasOk 报告相应的表达式是否可以在逗号-ok 赋值的右侧使用。
(TypeAndValue) IsBuiltin
|
|
IsBuiltin reports whether the corresponding expression denotes a (possibly parenthesized) built-in function.
IsBuiltin 报告相应的表达式是否表示(可能带括号的)内置函数。
(TypeAndValue) IsNil
|
|
IsNil reports whether the corresponding expression denotes the predeclared value nil.
IsNil 报告相应的表达式是否表示预先声明的值 nil。
(TypeAndValue) IsType
|
|
IsType reports whether the corresponding expression specifies a type.
IsType 报告相应的表达式是否指定了一个类型。
(TypeAndValue) IsValue
|
|
IsValue reports whether the corresponding expression is a value. Builtins are not considered values. Constant values have a non- nil Value.
IsValue 报告相应的表达式是否为值。内置函数不被视为值。常量值具有非 nil 值。
(TypeAndValue) IsVoid
|
|
IsVoid reports whether the corresponding expression is a function call without results.
IsVoid 报告相应的表达式是否是一个没有结果的函数调用。
type TypeList <- go1.18
|
|
TypeList holds a list of types.
TypeList 持有一系列类型。
(*TypeList) At <- go1.18
|
|
At returns the i’th type in the list.
At 返回列表中的第 i 个类型。
(*TypeList) Len <- go1.18
|
|
Len returns the number of types in the list. It is safe to call on a nil receiver.
Len 返回列表中的类型数。对 nil 接收器调用是安全的。
type TypeName
|
|
A TypeName represents a name for a (defined or alias) type.
TypeName 表示(已定义或别名)类型的名称。
func NewTypeName
|
|
NewTypeName returns a new type name denoting the given typ. The remaining arguments set the attributes found with all Objects.
NewTypeName 返回一个新的类型名称,表示给定的 typ。其余参数设置在所有对象中找到的属性。
The typ argument may be a defined (Named) type or an alias type. It may also be nil such that the returned TypeName can be used as argument for NewNamed, which will set the TypeName’s type as a side- effect.
typ 参数可以是已定义的(命名)类型或别名类型。它也可以是 nil,以便返回的 TypeName 可用作 NewNamed 的参数,这将把 TypeName 的类型设置为副作用。
(*TypeName) Exported
|
|
Exported reports whether the object is exported (starts with a capital letter). It doesn’t take into account whether the object is in a local (function) scope or not.
Exported 报告对象是否已导出(以大写字母开头)。它不考虑对象是否在本地(函数)范围内。
(*TypeName) Id
|
|
Id is a wrapper for Id(obj.Pkg(), obj.Name()).
Id 是 Id(obj.Pkg(), obj.Name()) 的包装器。
(*TypeName) IsAlias <- go1.9
|
|
IsAlias reports whether obj is an alias name for a type.
IsAlias 报告 obj 是否是类型的别名。
(*TypeName) Name
|
|
Name returns the object’s (package-local, unqualified) name.
Name 返回对象(包本地、不合格)的名称。
(*TypeName) Parent
|
|
Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.
Parent 返回声明对象的作用域。对于方法和结构字段,结果为 nil。
(*TypeName) Pkg
|
|
Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.
Pkg 返回对象所属的包。对于 Universe 范围内的标签和对象,结果为 nil。
(*TypeName) Pos
|
|
Pos returns the declaration position of the object’s identifier.
Pos 返回对象标识符的声明位置。
(*TypeName) String
|
|
(*TypeName) Type
|
|
Type returns the object’s type.
Type 返回对象类型。
type TypeParam <- go1.18
|
|
A TypeParam represents a type parameter type.
A TypeParam 表示类型参数类型。
func NewTypeParam <- go1.18
|
|
NewTypeParam returns a new TypeParam. Type parameters may be set on a Named or Signature type by calling SetTypeParams. Setting a type parameter on more than one type will result in a panic.
NewTypeParam 返回一个新的 TypeParam。可以通过调用 SetTypeParams 在 Named 或 Signature 类型上设置类型参数。在多个类型上设置类型参数将导致恐慌。
The constraint argument can be nil, and set later via SetConstraint. If the constraint is non-nil, it must be fully defined.
约束参数可以为 nil,并通过 SetConstraint 稍后设置。如果约束不是 nil,则必须完全定义。
(*TypeParam) Constraint <- go1.18
|
|
Constraint returns the type constraint specified for t.
约束返回为 t 指定的类型约束。
(*TypeParam) Index <- go1.18
|
|
Index returns the index of the type param within its param list, or -1 if the type parameter has not yet been bound to a type.
Index 返回类型参数在其参数列表中的索引,如果类型参数尚未绑定到类型,则返回 -1。
(*TypeParam) Obj <- go1.18
|
|
Obj returns the type name for t.
Obj 返回 t 的类型名称。
(*TypeParam) SetConstraint <- go1.18
|
|
SetConstraint sets the type constraint for t.
SetConstraint 为 t 设置类型约束。
It must be called by users of NewTypeParam after the bound’s underlying is fully defined, and before using the type parameter in any way other than to form other types. Once SetConstraint returns the receiver, t is safe for concurrent use.
在完全定义了 bound 的基础之后,并且在以形成其他类型以外的任何方式使用类型参数之前,NewTypeParam 的用户必须调用它。一旦 SetConstraint 返回接收器,t 就可安全地并发使用。
(*TypeParam) String <- go1.18
|
|
(*TypeParam) Underlying <- go1.18
|
|
type TypeParamList <- go1.18
|
|
TypeParamList holds a list of type parameters.
TypeParamList 包含一个类型参数列表。
(*TypeParamList) At <- go1.18
|
|
At returns the i’th type parameter in the list.
At 返回列表中的第 i 个类型参数。
(*TypeParamList) Len <- go1.18
|
|
Len returns the number of type parameters in the list. It is safe to call on a nil receiver.
Len 返回列表中的类型参数数量。对 nil 接收器调用是安全的。
type Union <- go1.18
|
|
A Union represents a union of terms embedded in an interface.
联合表示嵌入在接口中的项的联合。
func NewUnion <- go1.18
|
|
NewUnion returns a new Union type with the given terms. It is an error to create an empty union; they are syntactically not possible.
NewUnion 返回具有给定项的新 Union 类型。创建空联合是一种错误;它们在语法上是不可能的。
(*Union) Len <- go1.18
|
|
(*Union) String <- go1.18
|
|
(*Union) Term <- go1.18
|
|
(*Union) Underlying <- go1.18
|
|
type Var
|
|
A Variable represents a declared variable (including function parameters and results, and struct fields).
变量表示已声明的变量(包括函数参数和结果,以及结构字段)。
func NewField
|
|
NewField returns a new variable representing a struct field. For embedded fields, the name is the unqualified type name under which the field is accessible.
NewField 返回一个表示结构字段的新变量。对于嵌入字段,名称是字段可访问的非限定类型名称。
func NewParam
|
|
NewParam returns a new variable representing a function parameter.
NewParam 返回一个表示函数参数的新变量。
func NewVar
|
|
NewVar returns a new variable. The arguments set the attributes found with all Objects.
NewVar 返回一个新变量。参数设置所有对象中发现的属性。
(*Var) Anonymous
|
|
Anonymous reports whether the variable is an embedded field. Same as Embedded; only present for backward-compatibility.
Anonymous 报告变量是否为嵌入字段。与 Embedded 相同;仅出于向后兼容性而存在。
(*Var) Embedded <- go1.11
|
|
Embedded reports whether the variable is an embedded field.
Embedded 报告变量是否为嵌入字段。
(*Var) Exported
|
|
Exported reports whether the object is exported (starts with a capital letter). It doesn’t take into account whether the object is in a local (function) scope or not.
Exported 报告对象是否已导出(以大写字母开头)。它不考虑对象是否在本地(函数)作用域中。
(*Var) Id
|
|
Id is a wrapper for Id(obj.Pkg(), obj.Name()).
Id 是 Id(obj.Pkg(), obj.Name()) 的包装器。
(*Var) IsField
|
|
IsField reports whether the variable is a struct field.
IsField 报告变量是否为结构字段。
(*Var) Name
|
|
Name returns the object’s (package-local, unqualified) name.
Name 返回对象(包本地、不合格)的名称。
(*Var) Origin <- go1.19
|
|
Origin returns the canonical Var for its receiver, i.e. the Var object recorded in Info.Defs.
Origin 返回其接收器的规范 Var,即 Info.Defs 中记录的 Var 对象。
For synthetic Vars created during instantiation (such as struct fields or function parameters that depend on type arguments), this will be the corresponding Var on the generic (uninstantiated) type. For all other Vars Origin returns the receiver.
对于在实例化期间创建的合成 Var(例如,取决于类型参数的结构字段或函数参数),这将是泛型(未实例化)类型上的相应 Var。对于所有其他 Var,Origin 返回接收器。
(*Var) Parent
|
|
Parent returns the scope in which the object is declared. The result is nil for methods and struct fields.
Parent 返回声明对象的作用域。对于方法和结构字段,结果为 nil。
(*Var) Pkg
|
|
Pkg returns the package to which the object belongs. The result is nil for labels and objects in the Universe scope.
Pkg 返回对象所属的包。对于 Universe 范围内的标签和对象,结果为 nil。
(*Var) Pos
|
|
Pos returns the declaration position of the object’s identifier.
Pos 返回对象标识符的声明位置。
(*Var) String
|
|
(*Var) Type
|
|
Type returns the object’s type.
Type 返回对象类型。