Package types

import "go/types"
Overview
Index
Examples

Overview ▾

包类型声明数据类型并实现Go包类型检查的算法. 使用Config.Check调用包的类型检查器. 或者,使用NewChecker创建一个新的类型检查器,然后通过调用Checker.Files增量调用它.

类型检查包含几个相互依赖的阶段:

名称解析将程序中的每个标识符(ast.Ident)映射到它表示的语言对象(Object). 使用Info.{Defs,Uses,Implicits}获取名称解析结果.

常量折叠为作为编译时常量的每个表达式(ast.Expr)计算确切的常量值(constant.Value). 将Info.Types [expr] .Value用于常量折叠的结果.

类型推断计算每个表达式(ast.Expr)的类型(Type),并检查是否符合语言规范. 使用Info.Types [expr] .Type作为类型推断的结果.

有关教程,请参见https://golang.org/s/types-tutorial .

Index ▾

Variables
func AssertableTo(V *Interface, T Type) bool
func AssignableTo(V, T Type) bool
func CheckExpr(fset *token.FileSet, pkg *Package, pos token.Pos, expr ast.Expr, info *Info) (err error)
func Comparable(T Type) bool
func ConvertibleTo(V, T Type) bool
func DefPredeclaredTestFuncs()
func ExprString(x ast.Expr) string
func Id(pkg *Package, name string) string
func Identical(x, y Type) bool
func IdenticalIgnoreTags(x, y Type) bool
func Implements(V Type, T *Interface) bool
func IsInterface(typ Type) bool
func ObjectString(obj Object, qf Qualifier) string
func SelectionString(s *Selection, qf Qualifier) string
func TypeString(typ Type, qf Qualifier) string
func WriteExpr(buf *bytes.Buffer, x ast.Expr)
func WriteSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier)
func WriteType(buf *bytes.Buffer, typ Type, qf Qualifier)
type Array
    func NewArray(elem Type, len int64) *Array
    func (a *Array) Elem() Type
    func (a *Array) Len() int64
    func (a *Array) String() string
    func (a *Array) Underlying() Type
type Basic
    func (b *Basic) Info() BasicInfo
    func (b *Basic) Kind() BasicKind
    func (b *Basic) Name() string
    func (b *Basic) String() string
    func (b *Basic) Underlying() Type
type BasicInfo
type BasicKind
type Builtin
    func (obj *Builtin) Exported() bool
    func (obj *Builtin) Id() string
    func (obj *Builtin) Name() string
    func (obj *Builtin) Parent() *Scope
    func (obj *Builtin) Pkg() *Package
    func (obj *Builtin) Pos() token.Pos
    func (obj *Builtin) String() string
    func (obj *Builtin) Type() Type
type Chan
    func NewChan(dir ChanDir, elem Type) *Chan
    func (c *Chan) Dir() ChanDir
    func (c *Chan) Elem() Type
    func (c *Chan) String() string
    func (c *Chan) Underlying() Type
type ChanDir
type Checker
    func NewChecker(conf *Config, fset *token.FileSet, pkg *Package, info *Info) *Checker
    func (check *Checker) Files(files []*ast.File) error
type Config
    func (conf *Config) Check(path string, fset *token.FileSet, files []*ast.File, info *Info) (*Package, error)
type Const
    func NewConst(pos token.Pos, pkg *Package, name string, typ Type, val constant.Value) *Const
    func (obj *Const) Exported() bool
    func (obj *Const) Id() string
    func (obj *Const) Name() string
    func (obj *Const) Parent() *Scope
    func (obj *Const) Pkg() *Package
    func (obj *Const) Pos() token.Pos
    func (obj *Const) String() string
    func (obj *Const) Type() Type
    func (obj *Const) Val() constant.Value
type Error
    func (err Error) Error() string
type Func
    func MissingMethod(V Type, T *Interface, static bool) (method *Func, wrongType bool)
    func NewFunc(pos token.Pos, pkg *Package, name string, sig *Signature) *Func
    func (obj *Func) Exported() bool
    func (obj *Func) FullName() string
    func (obj *Func) Id() string
    func (obj *Func) Name() string
    func (obj *Func) Parent() *Scope
    func (obj *Func) Pkg() *Package
    func (obj *Func) Pos() token.Pos
    func (obj *Func) Scope() *Scope
    func (obj *Func) String() string
    func (obj *Func) Type() Type
type ImportMode
type Importer
type ImporterFrom
type Info
    func (info *Info) ObjectOf(id *ast.Ident) Object
    func (info *Info) TypeOf(e ast.Expr) Type
type Initializer
    func (init *Initializer) String() string
type Interface
    func NewInterface(methods []*Func, embeddeds []*Named) *Interface
    func NewInterfaceType(methods []*Func, embeddeds []Type) *Interface
    func (t *Interface) Complete() *Interface
    func (t *Interface) Embedded(i int) *Named
    func (t *Interface) EmbeddedType(i int) Type
    func (t *Interface) Empty() bool
    func (t *Interface) ExplicitMethod(i int) *Func
    func (t *Interface) Method(i int) *Func
    func (t *Interface) NumEmbeddeds() int
    func (t *Interface) NumExplicitMethods() int
    func (t *Interface) NumMethods() int
    func (t *Interface) String() string
    func (t *Interface) Underlying() Type
type Label
    func NewLabel(pos token.Pos, pkg *Package, name string) *Label
    func (obj *Label) Exported() bool
    func (obj *Label) Id() string
    func (obj *Label) Name() string
    func (obj *Label) Parent() *Scope
    func (obj *Label) Pkg() *Package
    func (obj *Label) Pos() token.Pos
    func (obj *Label) String() string
    func (obj *Label) Type() Type
type Map
    func NewMap(key, elem Type) *Map
    func (m *Map) Elem() Type
    func (m *Map) Key() Type
    func (m *Map) String() string
    func (m *Map) Underlying() Type
type MethodSet
    func NewMethodSet(T Type) *MethodSet
    func (s *MethodSet) At(i int) *Selection
    func (s *MethodSet) Len() int
    func (s *MethodSet) Lookup(pkg *Package, name string) *Selection
    func (s *MethodSet) String() string
type Named
    func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named
    func (t *Named) AddMethod(m *Func)
    func (t *Named) Method(i int) *Func
    func (t *Named) NumMethods() int
    func (t *Named) Obj() *TypeName
    func (t *Named) SetUnderlying(underlying Type)
    func (t *Named) String() string
    func (t *Named) Underlying() Type
type Nil
    func (obj *Nil) Exported() bool
    func (obj *Nil) Id() string
    func (obj *Nil) Name() string
    func (obj *Nil) Parent() *Scope
    func (obj *Nil) Pkg() *Package
    func (obj *Nil) Pos() token.Pos
    func (obj *Nil) String() string
    func (obj *Nil) Type() Type
type Object
    func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool)
type Package
    func NewPackage(path, name string) *Package
    func (pkg *Package) Complete() bool
    func (pkg *Package) Imports() []*Package
    func (pkg *Package) MarkComplete()
    func (pkg *Package) Name() string
    func (pkg *Package) Path() string
    func (pkg *Package) Scope() *Scope
    func (pkg *Package) SetImports(list []*Package)
    func (pkg *Package) SetName(name string)
    func (pkg *Package) String() string
type PkgName
    func NewPkgName(pos token.Pos, pkg *Package, name string, imported *Package) *PkgName
    func (obj *PkgName) Exported() bool
    func (obj *PkgName) Id() string
    func (obj *PkgName) Imported() *Package
    func (obj *PkgName) Name() string
    func (obj *PkgName) Parent() *Scope
    func (obj *PkgName) Pkg() *Package
    func (obj *PkgName) Pos() token.Pos
    func (obj *PkgName) String() string
    func (obj *PkgName) Type() Type
type Pointer
    func NewPointer(elem Type) *Pointer
    func (p *Pointer) Elem() Type
    func (p *Pointer) String() string
    func (p *Pointer) Underlying() Type
type Qualifier
    func RelativeTo(pkg *Package) Qualifier
type Scope
    func NewScope(parent *Scope, pos, end token.Pos, comment string) *Scope
    func (s *Scope) Child(i int) *Scope
    func (s *Scope) Contains(pos token.Pos) bool
    func (s *Scope) End() token.Pos
    func (s *Scope) Innermost(pos token.Pos) *Scope
    func (s *Scope) Insert(obj Object) Object
    func (s *Scope) Len() int
    func (s *Scope) Lookup(name string) Object
    func (s *Scope) LookupParent(name string, pos token.Pos) (*Scope, Object)
    func (s *Scope) Names() []string
    func (s *Scope) NumChildren() int
    func (s *Scope) Parent() *Scope
    func (s *Scope) Pos() token.Pos
    func (s *Scope) String() string
    func (s *Scope) WriteTo(w io.Writer, n int, recurse bool)
type Selection
    func (s *Selection) Index() []int
    func (s *Selection) Indirect() bool
    func (s *Selection) Kind() SelectionKind
    func (s *Selection) Obj() Object
    func (s *Selection) Recv() Type
    func (s *Selection) String() string
    func (s *Selection) Type() Type
type SelectionKind
type Signature
    func NewSignature(recv *Var, params, results *Tuple, variadic bool) *Signature
    func (s *Signature) Params() *Tuple
    func (s *Signature) Recv() *Var
    func (s *Signature) Results() *Tuple
    func (s *Signature) String() string
    func (s *Signature) Underlying() Type
    func (s *Signature) Variadic() bool
type Sizes
    func SizesFor(compiler, arch string) Sizes
type Slice
    func NewSlice(elem Type) *Slice
    func (s *Slice) Elem() Type
    func (s *Slice) String() string
    func (s *Slice) Underlying() Type
type StdSizes
    func (s *StdSizes) Alignof(T Type) int64
    func (s *StdSizes) Offsetsof(fields []*Var) []int64
    func (s *StdSizes) Sizeof(T Type) int64
type Struct
    func NewStruct(fields []*Var, tags []string) *Struct
    func (s *Struct) Field(i int) *Var
    func (s *Struct) NumFields() int
    func (s *Struct) String() string
    func (s *Struct) Tag(i int) string
    func (s *Struct) Underlying() Type
type Tuple
    func NewTuple(x ...*Var) *Tuple
    func (t *Tuple) At(i int) *Var
    func (t *Tuple) Len() int
    func (t *Tuple) String() string
    func (t *Tuple) Underlying() Type
type Type
    func Default(typ Type) Type
type TypeAndValue
    func Eval(fset *token.FileSet, pkg *Package, pos token.Pos, expr string) (_ TypeAndValue, err error)
    func (tv TypeAndValue) Addressable() bool
    func (tv TypeAndValue) Assignable() bool
    func (tv TypeAndValue) HasOk() bool
    func (tv TypeAndValue) IsBuiltin() bool
    func (tv TypeAndValue) IsNil() bool
    func (tv TypeAndValue) IsType() bool
    func (tv TypeAndValue) IsValue() bool
    func (tv TypeAndValue) IsVoid() bool
type TypeName
    func NewTypeName(pos token.Pos, pkg *Package, name string, typ Type) *TypeName
    func (obj *TypeName) Exported() bool
    func (obj *TypeName) Id() string
    func (obj *TypeName) IsAlias() bool
    func (obj *TypeName) Name() string
    func (obj *TypeName) Parent() *Scope
    func (obj *TypeName) Pkg() *Package
    func (obj *TypeName) Pos() token.Pos
    func (obj *TypeName) String() string
    func (obj *TypeName) Type() Type
type Var
    func NewField(pos token.Pos, pkg *Package, name string, typ Type, embedded bool) *Var
    func NewParam(pos token.Pos, pkg *Package, name string, typ Type) *Var
    func NewVar(pos token.Pos, pkg *Package, name string, typ Type) *Var
    func (obj *Var) Anonymous() bool
    func (obj *Var) Embedded() bool
    func (obj *Var) Exported() bool
    func (obj *Var) Id() string
    func (obj *Var) IsField() bool
    func (obj *Var) Name() string
    func (obj *Var) Parent() *Scope
    func (obj *Var) Pkg() *Package
    func (obj *Var) Pos() token.Pos
    func (obj *Var) String() string
    func (obj *Var) Type() Type

Examples

Info
MethodSet
Scope

Package files

api.go assignments.go builtins.go call.go check.go conversions.go decl.go errors.go eval.go expr.go exprstring.go gccgosizes.go initorder.go interfaces.go labels.go lookup.go methodset.go object.go objset.go operand.go package.go predicates.go resolver.go return.go scope.go selection.go sizes.go stmt.go type.go typestring.go typexpr.go universe.go

Variables

Typ包含预先声明的* Basic类型,它们由相应的BasicKind索引.

Typ [Byte]的*基本类型的名称为" uint8". 使用Universe.Lookup(" byte").Type()获得名为" byte"的特定别名基本类型(与" rune"类似).

var Typ = []*Basic{
    Invalid: {Invalid, 0, "invalid type"},

    Bool:          {Bool, IsBoolean, "bool"},
    Int:           {Int, IsInteger, "int"},
    Int8:          {Int8, IsInteger, "int8"},
    Int16:         {Int16, IsInteger, "int16"},
    Int32:         {Int32, IsInteger, "int32"},
    Int64:         {Int64, IsInteger, "int64"},
    Uint:          {Uint, IsInteger | IsUnsigned, "uint"},
    Uint8:         {Uint8, IsInteger | IsUnsigned, "uint8"},
    Uint16:        {Uint16, IsInteger | IsUnsigned, "uint16"},
    Uint32:        {Uint32, IsInteger | IsUnsigned, "uint32"},
    Uint64:        {Uint64, IsInteger | IsUnsigned, "uint64"},
    Uintptr:       {Uintptr, IsInteger | IsUnsigned, "uintptr"},
    Float32:       {Float32, IsFloat, "float32"},
    Float64:       {Float64, IsFloat, "float64"},
    Complex64:     {Complex64, IsComplex, "complex64"},
    Complex128:    {Complex128, IsComplex, "complex128"},
    String:        {String, IsString, "string"},
    UnsafePointer: {UnsafePointer, 0, "Pointer"},

    UntypedBool:    {UntypedBool, IsBoolean | IsUntyped, "untyped bool"},
    UntypedInt:     {UntypedInt, IsInteger | IsUntyped, "untyped int"},
    UntypedRune:    {UntypedRune, IsInteger | IsUntyped, "untyped rune"},
    UntypedFloat:   {UntypedFloat, IsFloat | IsUntyped, "untyped float"},
    UntypedComplex: {UntypedComplex, IsComplex | IsUntyped, "untyped complex"},
    UntypedString:  {UntypedString, IsString | IsUntyped, "untyped string"},
    UntypedNil:     {UntypedNil, IsUntyped, "untyped nil"},
}

func AssertableTo 1.5

func AssertableTo(V *Interface, T Type) bool

AssertableTo报告是否可以断言类型V的值具有类型T.

func AssignableTo 1.5

func AssignableTo(V, T Type) bool

AssignableTo报告类型V的值是否可分配给类型T的变量.

func CheckExpr 1.13

func CheckExpr(fset *token.FileSet, pkg *Package, pos token.Pos, expr ast.Expr, info *Info) (err error)

CheckExpr类型检查表达式expr是否好像出现在软件包pkg的位置pos上. 有关表达式的类型信息记录在info中.

如果pkg == nil,则使用Universe范围,并且忽略提供的位置pos. 如果pkg!= nil,而pos无效,则使用包范围. 否则,pos必须属于该软件包.

如果pos不在软件包中,或者无法对节点进行类型检查,则返回错误.

注意:不应使用Eval和CheckExpr而不是运行Check来计算类型和值,而是使用Check,因为这些函数会忽略使用表达式的上下文(例如,赋值). 因此,顶级无类型常量将返回无类型类型,而不是相应的上下文特定类型.

func Comparable 1.5

func Comparable(T Type) bool

可比较报告类型T的值是否可比较.

func ConvertibleTo 1.5

func ConvertibleTo(V, T Type) bool

ConvertibleTo报告类型V的值是否可转换为类型T的值.

func DefPredeclaredTestFuncs 1.5

func DefPredeclaredTestFuncs()

DefPredeclaredTestFuncs定义断言和跟踪内置函数. 这些内置程序仅用于调试和测试此程序包.

func ExprString 1.5

func ExprString(x ast.Expr) string

ExprString返回x的(可能是缩短的)字符串表示形式. 缩短的表示形式适用于用户界面,但不一定遵循Go语法.

func Id 1.5

func Id(pkg *Package, name string) string

如果已导出,则Id返回名称,否则返回具有包路径限定的名称.

func Identical 1.5

func Identical(x, y Type) bool

相同地报告x和y是否为相同类型. 签名类型的接收者将被忽略.

func IdenticalIgnoreTags 1.8

func IdenticalIgnoreTags(x, y Type) bool

如果忽略标签,则IdenticalIgnoreTags报告x和y是否为相同类型. 签名类型的接收者将被忽略.

func Implements 1.5

func Implements(V Type, T *Interface) bool

实现报告类型V是否实现接口T.

func IsInterface 1.5

func IsInterface(typ Type) bool

IsInterface报告typ是否为接口类型.

func ObjectString 1.5

func ObjectString(obj Object, qf Qualifier) string

ObjectString返回obj的字符串形式. 限定符控制包级对象的打印,并且可以为零.

func SelectionString 1.5

func SelectionString(s *Selection, qf Qualifier) string

SelectionString返回s的字符串形式. 限定符控制包级对象的打印,并且可以为零.

Examples:

"field (T) f int"
"method (T) f(X) Y"
"method expr (T) f(X) Y"

func TypeString 1.5

func TypeString(typ Type, qf Qualifier) string

TypeString返回typ的字符串表示形式. 限定符控制包级对象的打印,并且可以为零.

func WriteExpr 1.5

func WriteExpr(buf *bytes.Buffer, x ast.Expr)

WriteExpr将x的(可能是缩短的)字符串表示形式写入buf. 缩短的表示形式适用于用户界面,但不一定遵循Go语法.

func WriteSignature 1.5

func WriteSignature(buf *bytes.Buffer, sig *Signature, qf Qualifier)

WriteSignature将签名sig的表示形式写入buf,而没有前导的" func"关键字. 限定符控制包级对象的打印,并且可以为零.

func WriteType 1.5

func WriteType(buf *bytes.Buffer, typ Type, qf Qualifier)

WriteType将typ的字符串表示形式写入buf. 限定符控制包级对象的打印,并且可以为零.

type Array 1.5

数组表示数组类型.

type Array struct {
    // contains filtered or unexported fields
}

func NewArray 1.5

func NewArray(elem Type, len int64) *Array

NewArray返回给定元素类型和长度的新数组类型. 负长度表示未知长度.

func (*Array) Elem 1.5

func (a *Array) Elem() Type

Elem返回数组a的元素类型.

func (*Array) Len 1.5

func (a *Array) Len() int64

Len返回数组a的长度. 负结果表示长度未知.

func (*Array) String 1.5

func (a *Array) String() string

func (*Array) Underlying 1.5

func (a *Array) Underlying() Type

type Basic 1.5

基本表示基本类型.

type Basic struct {
    // contains filtered or unexported fields
}

func (*Basic) Info 1.5

func (b *Basic) Info() BasicInfo

信息返回有关基本类型b的属性的信息.

func (*Basic) Kind 1.5

func (b *Basic) Kind() BasicKind

Kind返回基本类型b的类型.

func (*Basic) Name 1.5

func (b *Basic) Name() string

Name返回基本类型b的名称.

func (*Basic) String 1.5

func (b *Basic) String() string

func (*Basic) Underlying 1.5

func (b *Basic) Underlying() Type

type BasicInfo 1.5

BasicInfo是一组描述基本类型属性的标志.

type BasicInfo int

基本类型的属性.

const (
    IsBoolean BasicInfo = 1 << iota
    IsInteger
    IsUnsigned
    IsFloat
    IsComplex
    IsString
    IsUntyped

    IsOrdered   = IsInteger | IsFloat | IsString
    IsNumeric   = IsInteger | IsFloat | IsComplex
    IsConstType = IsBoolean | IsNumeric | IsString
)

type BasicKind 1.5

BasicKind描述基本类型的种类.

type BasicKind int
const (
    Invalid BasicKind = iota // type is invalid

    // predeclared types
    Bool
    Int
    Int8
    Int16
    Int32
    Int64
    Uint
    Uint8
    Uint16
    Uint32
    Uint64
    Uintptr
    Float32
    Float64
    Complex64
    Complex128
    String
    UnsafePointer

    // types for untyped values
    UntypedBool
    UntypedInt
    UntypedRune
    UntypedFloat
    UntypedComplex
    UntypedString
    UntypedNil

    // aliases
    Byte = Uint8
    Rune = Int32
)

type Builtin 1.5

内置表示内置功能. Builtins没有有效的类型.

type Builtin struct {
    // contains filtered or unexported fields
}

func (*Builtin) Exported 1.5

func (obj *Builtin) Exported() bool

导出报告对象是否已导出(以大写字母开头). 它不考虑对象是否在本地(功能)范围内.

func (*Builtin) Id 1.5

func (obj *Builtin) Id() string

Id是Id(obj.Pkg(),obj.Name())的包装.

func (*Builtin) Name 1.5

func (obj *Builtin) Name() string

Name返回对象的名称(本地程序包,不合格).

func (*Builtin) Parent 1.5

func (obj *Builtin) Parent() *Scope

父级返回声明对象的范围. 方法和结构字段的结果为零.

func (*Builtin) Pkg 1.5

func (obj *Builtin) Pkg() *Package

Pkg返回对象所属的包. 对于Universe范围中的标签和对象,结果为nil.

func (*Builtin) Pos 1.5

func (obj *Builtin) Pos() token.Pos

Pos返回对象标识符的声明位置.

func (*Builtin) String 1.5

func (obj *Builtin) String() string

func (*Builtin) Type 1.5

func (obj *Builtin) Type() Type

Type返回对象的类型.

type Chan 1.5

Chan代表渠道类型.

type Chan struct {
    // contains filtered or unexported fields
}

func NewChan 1.5

func NewChan(dir ChanDir, elem Type) *Chan

NewChan返回给定方向和元素类型的新通道类型.

func (*Chan) Dir 1.5

func (c *Chan) Dir() ChanDir

Dir returns the direction of channel c.

func (*Chan) Elem 1.5

func (c *Chan) Elem() Type

Elem返回通道c的元素类型.

func (*Chan) String 1.5

func (c *Chan) String() string

func (*Chan) Underlying 1.5

func (c *Chan) Underlying() Type

type ChanDir 1.5

ChanDir值指示通道方向.

type ChanDir int

通道的方向由这些常数之一指示.

const (
    SendRecv ChanDir = iota
    SendOnly
    RecvOnly
)

type Checker 1.5

检查器维护类型检查器的状态. 必须使用NewChecker创建它.

type Checker struct {
    *Info
    // contains filtered or unexported fields
}

func NewChecker 1.5

func NewChecker(conf *Config, fset *token.FileSet, pkg *Package, info *Info) *Checker

NewChecker返回给定包的新Checker实例. 软件包文件可以通过checker.Files增量添加.

func (*Checker) Files 1.5

func (check *Checker) Files(files []*ast.File) error

文件检查作为检查程序包一部分的提供的文件.

type Config 1.5

Config指定用于类型检查的配置. Config的零值是随时可用的默认配置.

type Config struct {
    // If IgnoreFuncBodies is set, function bodies are not
    // type-checked.
    IgnoreFuncBodies bool

    // If FakeImportC is set, `import "C"` (for packages requiring Cgo)
    // declares an empty "C" package and errors are omitted for qualified
    // identifiers referring to package C (which won't find an object).
    // This feature is intended for the standard library cmd/api tool.
    //
    // Caution: Effects may be unpredictable due to follow-on errors.
    //          Do not use casually!
    FakeImportC bool

    // If Error != nil, it is called with each error found
    // during type checking; err has dynamic type Error.
    // Secondary errors (for instance, to enumerate all types
    // involved in an invalid recursive type declaration) have
    // error strings that start with a '\t' character.
    // If Error == nil, type-checking stops with the first
    // error found.
    Error func(err error)

    // An importer is used to import packages referred to from
    // import declarations.
    // If the installed importer implements ImporterFrom, the type
    // checker calls ImportFrom instead of Import.
    // The type checker reports an error if an importer is needed
    // but none was installed.
    Importer Importer

    // If Sizes != nil, it provides the sizing functions for package unsafe.
    // Otherwise SizesFor("gc", "amd64") is used instead.
    Sizes Sizes

    // If DisableUnusedImportCheck is set, packages are not checked
    // for unused imports.
    DisableUnusedImportCheck bool
}

func (*Config) Check 1.5

func (conf *Config) Check(path string, fset *token.FileSet, files []*ast.File, info *Info) (*Package, error)

Check对软件包进行类型检查,并返回生成的软件包对象和第一个错误(如果有). 此外,如果info!= nil,则Check将填充Info结构中的每个非nil映射.

如果没有发生错误,则该程序包将标记为已完成,否则为不完整. 有关在出现错误时控制行为的信息,请参见Config.Error.

包由* ast.Files和相应文件集的列表指定,并使用该包标识包的路径. 干净路径不能为空或点(".").

type Const 1.5

常量代表声明的常量.

type Const struct {
    // contains filtered or unexported fields
}

func NewConst 1.5

func NewConst(pos token.Pos, pkg *Package, name string, typ Type, val constant.Value) *Const

NewConst返回值为val的新常数. 其余参数设置所有对象都有的属性.

func (*Const) Exported 1.5

func (obj *Const) Exported() bool

导出报告对象是否已导出(以大写字母开头). 它不考虑对象是否在本地(功能)范围内.

func (*Const) Id 1.5

func (obj *Const) Id() string

Id是Id(obj.Pkg(),obj.Name())的包装.

func (*Const) Name 1.5

func (obj *Const) Name() string

Name返回对象的名称(本地程序包,不合格).

func (*Const) Parent 1.5

func (obj *Const) Parent() *Scope

父级返回声明对象的范围. 方法和结构字段的结果为零.

func (*Const) Pkg 1.5

func (obj *Const) Pkg() *Package

Pkg返回对象所属的包. 对于Universe范围中的标签和对象,结果为nil.

func (*Const) Pos 1.5

func (obj *Const) Pos() token.Pos

Pos返回对象标识符的声明位置.

func (*Const) String 1.5

func (obj *Const) String() string

func (*Const) Type 1.5

func (obj *Const) Type() Type

Type返回对象的类型.

func (*Const) Val 1.5

func (obj *Const) Val() constant.Value

Val返回常数的值.

type Error 1.5

错误描述类型检查错误. 它实现了错误接口. "软"错误是仍然允许对包进行有效解释的错误(例如"未使用的变量"); 如果忽略"硬"错误,可能会导致无法预测的行为.

type Error struct {
    Fset *token.FileSet // file set for interpretation of Pos
    Pos  token.Pos      // error position
    Msg  string         // error message
    Soft bool           // if set, error is "soft"
}

func (Error) Error 1.5

func (err Error) Error() string

错误返回错误字符串,其格式如下:filename:line:column:message

type Func 1.5

Func代表声明的函数,具体方法或抽象(接口)方法. 它的Type()始终是* Signature. 由于嵌入,抽象方法可能属于许多接口.

type Func struct {
    // contains filtered or unexported fields
}

func MissingMethod 1.5

func MissingMethod(V Type, T *Interface, static bool) (method *Func, wrongType bool)

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,或者如果设置了static,则如果V中存在T的所有方法,则V实现T.否则(V是接口并且未设置静态),MissingMethod仅检查是否存在T的方法在V中具有匹配的类型(例如,对于类型断言x.(T),其中x为接口类型V).

func NewFunc 1.5

func NewFunc(pos token.Pos, pkg *Package, name string, sig *Signature) *Func

NewFunc返回具有给定签名的新函数,表示该函数的类型.

func (*Func) Exported 1.5

func (obj *Func) Exported() bool

导出报告对象是否已导出(以大写字母开头). 它不考虑对象是否在本地(功能)范围内.

func (*Func) FullName 1.5

func (obj *Func) FullName() string

FullName返回函数或方法obj的包或接收者类型限定的名称.

func (*Func) Id 1.5

func (obj *Func) Id() string

Id是Id(obj.Pkg(),obj.Name())的包装.

func (*Func) Name 1.5

func (obj *Func) Name() string

Name返回对象的名称(本地程序包,不合格).

func (*Func) Parent 1.5

func (obj *Func) Parent() *Scope

父级返回声明对象的范围. 方法和结构字段的结果为零.

func (*Func) Pkg 1.5

func (obj *Func) Pkg() *Package

Pkg返回对象所属的包. 对于Universe范围中的标签和对象,结果为nil.

func (*Func) Pos 1.5

func (obj *Func) Pos() token.Pos

Pos返回对象标识符的声明位置.

func (*Func) Scope 1.5

func (obj *Func) Scope() *Scope

范围返回函数的主体块的范围.

func (*Func) String 1.5

func (obj *Func) String() string

func (*Func) Type 1.5

func (obj *Func) Type() Type

Type返回对象的类型.

type ImportMode 1.6

ImportMode保留供将来使用.

type ImportMode int

type Importer 1.5

导入程序解析包的导入路径.

注意:此界面不支持导入本地供应的软件包. 请参阅https://golang.org/s/go15vendor . 如果可能,外部实现应实现ImporterFrom.

type Importer interface {
    // Import returns the imported package for the given import path.
    // The semantics is like for ImporterFrom.ImportFrom except that
    // dir and mode are ignored (since they are not present).
    Import(path string) (*Package, error)
}

type ImporterFrom 1.6

ImporterFrom解析包的导入路径; 它支持根据https://golang.org/s/go15vendor进行供应 . 使用go / importer获取ImporterFrom实现.

type ImporterFrom interface {
    // Importer is present for backward-compatibility. Calling
    // Import(path) is the same as calling ImportFrom(path, "", 0);
    // i.e., locally vendored packages may not be found.
    // The types package does not call Import if an ImporterFrom
    // is present.
    Importer

    // ImportFrom returns the imported package for the given import
    // path when imported by a package file located in dir.
    // If the import failed, besides returning an error, ImportFrom
    // is encouraged to cache and return a package anyway, if one
    // was created. This will reduce package inconsistencies and
    // follow-on type checker errors due to the missing package.
    // The mode value must be 0; it is reserved for future use.
    // Two calls to ImportFrom with the same path and dir must
    // return the same package.
    ImportFrom(path, dir string, mode ImportMode) (*Package, error)
}

type Info 1.5

信息保存类型检查包的结果类型信息. 仅收集提供地图的信息. 如果程序包有类型错误,则收集的信息可能不完整.

type Info struct {
    // Types maps expressions to their types, and for constant
    // expressions, also their values. Invalid expressions are
    // omitted.
    //
    // For (possibly parenthesized) identifiers denoting built-in
    // functions, the recorded signatures are call-site specific:
    // if the call result is not a constant, the recorded type is
    // an argument-specific signature. Otherwise, the recorded type
    // is invalid.
    //
    // The Types map does not record the type of every identifier,
    // only those that appear where an arbitrary expression is
    // permitted. For instance, the identifier f in a selector
    // expression x.f is found only in the Selections map, the
    // identifier z in a variable declaration 'var z int' is found
    // only in the Defs map, and identifiers denoting packages in
    // qualified identifiers are collected in the Uses map.
    Types map[ast.Expr]TypeAndValue

    // Defs maps identifiers to the objects they define (including
    // package names, dots "." of dot-imports, and blank "_" identifiers).
    // For identifiers that do not denote objects (e.g., the package name
    // in package clauses, or symbolic variables t in t := x.(type) of
    // type switch headers), the corresponding objects are nil.
    //
    // For an embedded field, Defs returns the field *Var it defines.
    //
    // Invariant: Defs[id] == nil || Defs[id].Pos() == id.Pos()
    Defs map[*ast.Ident]Object

    // Uses maps identifiers to the objects they denote.
    //
    // For an embedded field, Uses returns the *TypeName it denotes.
    //
    // Invariant: Uses[id].Pos() != id.Pos()
    Uses map[*ast.Ident]Object

    // Implicits maps nodes to their implicitly declared objects, if any.
    // The following node and object types may appear:
    //
    //     node               declared object
    //
    //     *ast.ImportSpec    *PkgName for imports without renames
    //     *ast.CaseClause    type-specific *Var for each type switch case clause (incl. default)
    //     *ast.Field         anonymous parameter *Var (incl. unnamed results)
    //
    Implicits map[ast.Node]Object

    // Selections maps selector expressions (excluding qualified identifiers)
    // to their corresponding selections.
    Selections map[*ast.SelectorExpr]*Selection

    // Scopes maps ast.Nodes to the scopes they define. Package scopes are not
    // associated with a specific node but with all files belonging to a package.
    // Thus, the package scope can be found in the type-checked Package object.
    // Scopes nest, with the Universe scope being the outermost scope, enclosing
    // the package scope, which contains (one or more) files scopes, which enclose
    // function scopes which in turn enclose statement and function literal scopes.
    // Note that even though package-level functions are declared in the package
    // scope, the function scopes are embedded in the file scope of the file
    // containing the function declaration.
    //
    // The following node types may appear in Scopes:
    //
    //     *ast.File
    //     *ast.FuncType
    //     *ast.BlockStmt
    //     *ast.IfStmt
    //     *ast.SwitchStmt
    //     *ast.TypeSwitchStmt
    //     *ast.CaseClause
    //     *ast.CommClause
    //     *ast.ForStmt
    //     *ast.RangeStmt
    //
    Scopes map[ast.Node]*Scope

    // InitOrder is the list of package-level initializers in the order in which
    // they must be executed. Initializers referring to variables related by an
    // initialization dependency appear in topological order, the others appear
    // in source order. Variables without an initialization expression do not
    // appear in this list.
    InitOrder []*Initializer
}

Example

ExampleInfo将类型检查器记录的各种事实打印为type.Info结构:每个命名对象的定义和引用,以及包中每个表达式的类型,值和模式.

InitOrder: [c = "hello" b = S(c) a = len(b)]

Defs and Uses of each named object:
builtin len:
  defined at -
  used at 6:15
func fib(x int) int:
  defined at fib.go:8:6
  used at 12:20, 12:9
type S string:
  defined at fib.go:4:6
  used at 6:23
type int:
  defined at -
  used at 8:12, 8:17
type string:
  defined at -
  used at 4:8
var b S:
  defined at fib.go:6:8
  used at 6:19
var c string:
  defined at fib.go:6:11
  used at 6:25
var x int:
  defined at fib.go:8:10
  used at 10:10, 12:13, 12:24, 9:5

Types and Values of each expression:
 4: 8 | string              | type    : string
 6:15 | len                 | builtin : func(string) int
 6:15 | len(b)              | value   : int
 6:19 | b                   | var     : fib.S
 6:23 | S                   | type    : fib.S
 6:23 | S(c)                | value   : fib.S
 6:25 | c                   | var     : string
 6:29 | "hello"             | value   : string = "hello"
 8:12 | int                 | type    : int
 8:17 | int                 | type    : int
 9: 5 | x                   | var     : int
 9: 5 | x < 2               | value   : untyped bool
 9: 9 | 2                   | value   : int = 2
10:10 | x                   | var     : int
12: 9 | fib                 | value   : func(x int) int
12: 9 | fib(x - 1)          | value   : int
12: 9 | fib(x-1) - fib(x-2) | value   : int
12:13 | x                   | var     : int
12:13 | x - 1               | value   : int
12:15 | 1                   | value   : int = 1
12:20 | fib                 | value   : func(x int) int
12:20 | fib(x - 2)          | value   : int
12:24 | x                   | var     : int
12:24 | x - 2               | value   : int
12:26 | 2                   | value   : int = 2

func (*Info) ObjectOf 1.5

func (info *Info) ObjectOf(id *ast.Ident) Object

ObjectOf返回由指定的id表示的对象;如果找不到,则返回nil.

如果id是嵌入式结构字段,则ObjectOf返回其定义的字段(* Var),而不是其使用的类型(* TypeName).

前提条件:已使用和定义地图.

func (*Info) TypeOf 1.5

func (info *Info) TypeOf(e ast.Expr) Type

TypeOf返回表达式e的类型;如果未找到,则返回nil. 前提条件:已填入类型,用途和定义图.

type Initializer 1.5

初始化程序描述程序包级变量,或在多值初始化表达式的情况下的变量列表,以及相应的初始化表达式.

type Initializer struct {
    Lhs []*Var // var Lhs = Rhs
    Rhs ast.Expr
}

func (*Initializer) String 1.5

func (init *Initializer) String() string

type Interface 1.5

接口表示接口类型.

type Interface struct {
    // contains filtered or unexported fields
}

func NewInterface 1.5

func NewInterface(methods []*Func, embeddeds []*Named) *Interface

NewInterface返回给定方法和嵌入类型的新(不完整)接口. 每个嵌入式类型必须具有接口类型的基础类型. NewInterface拥有所提供方法的所有权,并且可以通过设置缺少的接收者来修改其类型. 要计算接口的方法集,必须调用Complete.

不建议使用:改用NewInterfaceType,它可以嵌入任何(甚至是未定义的)接口类型. 对于嵌入引用未定义(文字)接口类型的别名类型名称的接口,这是必需的.

func NewInterfaceType 1.11

func NewInterfaceType(methods []*Func, embeddeds []Type) *Interface

NewInterfaceType返回给定方法和嵌入类型的新(不完整)接口. 每个嵌入式类型都必须具有接口类型的基础类型(尚未针对定义的类型验证此属性,该属性可能正在设置中,并且尚未具有有效的基础类型). NewInterfaceType拥有所提供方法的所有权,并可以通过设置缺少的接收者来修改其类型. 要计算接口的方法集,必须调用Complete.

func (*Interface) Complete 1.5

func (t *Interface) Complete() *Interface

Complete计算接口的方法集. 在完全定义接口的嵌入式类型之后,以及以除形成其他类型之外的任何其他方式使用接口类型之前,NewInterfaceType和NewInterface的用户必须调用它. 完成返回接收者.

func (*Interface) Embedded 1.5

func (t *Interface) Embedded(i int) *Named

对于0 <= i <t.NumEmbeddeds(),Embedded返回接口t的第i个嵌入式定义(*命名)类型. 如果第i个嵌入式类型不是定义的类型,则结果为nil.

不推荐使用:使用EmbeddedType,它不限于定义的(*命名)类型.

func (*Interface) EmbeddedType 1.11

func (t *Interface) EmbeddedType(i int) Type

EmbeddedType returns the i'th embedded type of interface t for 0 <= i < t.NumEmbeddeds().

func (*Interface) Empty 1.5

func (t *Interface) Empty() bool

空报告t是否为空接口.

func (*Interface) ExplicitMethod 1.5

func (t *Interface) ExplicitMethod(i int) *Func

ExplicitMethod返回接口t的第i个显式声明的方法,其取值为0 <= i <t.NumExplicitMethods(). 这些方法按其唯一ID排序.

func (*Interface) Method 1.5

func (t *Interface) Method(i int) *Func

方法为0 <= i <t.NumMethods()返回接口t的第i个方法. 这些方法按其唯一ID排序.

func (*Interface) NumEmbeddeds 1.5

func (t *Interface) NumEmbeddeds() int

NumEmbeddeds返回接口t中嵌入类型的数量.

func (*Interface) NumExplicitMethods 1.5

func (t *Interface) NumExplicitMethods() int

NumExplicitMethods返回接口t的显式声明的方法的数量.

func (*Interface) NumMethods 1.5

func (t *Interface) NumMethods() int

NumMethods返回接口t的方法总数.

func (*Interface) String 1.5

func (t *Interface) String() string

func (*Interface) Underlying 1.5

func (t *Interface) Underlying() Type

type Label 1.5

标签代表声明的标签. 标签没有类型.

type Label struct {
    // contains filtered or unexported fields
}

func NewLabel 1.5

func NewLabel(pos token.Pos, pkg *Package, name string) *Label

NewLabel返回一个新标签.

func (*Label) Exported 1.5

func (obj *Label) Exported() bool

导出报告对象是否已导出(以大写字母开头). 它不考虑对象是否在本地(功能)范围内.

func (*Label) Id 1.5

func (obj *Label) Id() string

Id是Id(obj.Pkg(),obj.Name())的包装.

func (*Label) Name 1.5

func (obj *Label) Name() string

Name返回对象的名称(本地程序包,不合格).

func (*Label) Parent 1.5

func (obj *Label) Parent() *Scope

父级返回声明对象的范围. 方法和结构字段的结果为零.

func (*Label) Pkg 1.5

func (obj *Label) Pkg() *Package

Pkg返回对象所属的包. 对于Universe范围中的标签和对象,结果为nil.

func (*Label) Pos 1.5

func (obj *Label) Pos() token.Pos

Pos返回对象标识符的声明位置.

func (*Label) String 1.5

func (obj *Label) String() string

func (*Label) Type 1.5

func (obj *Label) Type() Type

Type返回对象的类型.

type Map 1.5

地图代表地图类型.

type Map struct {
    // contains filtered or unexported fields
}

func NewMap 1.5

func NewMap(key, elem Type) *Map

NewMap返回给定键和元素类型的新映射.

func (*Map) Elem 1.5

func (m *Map) Elem() Type

Elem返回映射m的元素类型.

func (*Map) Key 1.5

func (m *Map) Key() Type

Key返回映射m的键类型.

func (*Map) String 1.5

func (m *Map) String() string

func (*Map) Underlying 1.5

func (m *Map) Underlying() Type

type MethodSet 1.5

MethodSet是具体或抽象(接口)方法的有序集合. 方法是MethodVal选择,它们通过将m.Obj().Id()升序进行排序. MethodSet的零值为可立即使用的空方法集.

type MethodSet struct {
    // contains filtered or unexported fields
}

Example

ExampleMethodSet打印各种类型的方法集.

Method set of temperature.Celsius:
method (temperature.Celsius) String() string

Method set of *temperature.Celsius:
method (*temperature.Celsius) SetF(f float64)
method (*temperature.Celsius) String() string

func NewMethodSet 1.5

func NewMethodSet(T Type) *MethodSet

NewMethodSet返回给定类型T的方法集.即使它为空,也总是返回非null的方法集.

func (*MethodSet) At 1.5

func (s *MethodSet) At(i int) *Selection

At返回0中的第i个方法,其中0 <= i <s.Len().

func (*MethodSet) Len 1.5

func (s *MethodSet) Len() int

Len返回以s为单位的方法数.

func (*MethodSet) Lookup 1.5

func (s *MethodSet) Lookup(pkg *Package, name string) *Selection

查找将返回具有匹配包和名称的方法,如果找不到,则返回nil.

func (*MethodSet) String 1.5

func (s *MethodSet) String() string

type Named 1.5

名称表示命名类型.

type Named struct {
    // contains filtered or unexported fields
}

func NewNamed 1.5

func NewNamed(obj *TypeName, underlying Type, methods []*Func) *Named

NewNamed返回给定类型名称,基础类型和关联方法的新命名类型. 如果给定的类型名称obj还没有类型,则将其类型设置为返回的命名类型. 基础类型不能为* Named.

func (*Named) AddMethod 1.5

func (t *Named) AddMethod(m *Func)

AddMethod会添加方法m,除非它已经在方法列表中.

func (*Named) Method 1.5

func (t *Named) Method(i int) *Func

方法返回0 <= i <t.NumMethods()的第i个命名类型为t的方法.

func (*Named) NumMethods 1.5

func (t *Named) NumMethods() int

NumMethods返回其接收者称为类型t的显式方法的数量.

func (*Named) Obj 1.5

func (t *Named) Obj() *TypeName

Obj返回命名类型t的类型名称.

func (*Named) SetUnderlying 1.5

func (t *Named) SetUnderlying(underlying Type)

SetUnderlying设置基础类型并将t标记为完成.

func (*Named) String 1.5

func (t *Named) String() string

func (*Named) Underlying 1.5

func (t *Named) Underlying() Type

type Nil 1.5

Nil代表预设值nil.

type Nil struct {
    // contains filtered or unexported fields
}

func (*Nil) Exported 1.5

func (obj *Nil) Exported() bool

导出报告对象是否已导出(以大写字母开头). 它不考虑对象是否在本地(功能)范围内.

func (*Nil) Id 1.5

func (obj *Nil) Id() string

Id是Id(obj.Pkg(),obj.Name())的包装.

func (*Nil) Name 1.5

func (obj *Nil) Name() string

Name返回对象的名称(本地程序包,不合格).

func (*Nil) Parent 1.5

func (obj *Nil) Parent() *Scope

父级返回声明对象的范围. 方法和结构字段的结果为零.

func (*Nil) Pkg 1.5

func (obj *Nil) Pkg() *Package

Pkg返回对象所属的包. 对于Universe范围中的标签和对象,结果为nil.

func (*Nil) Pos 1.5

func (obj *Nil) Pos() token.Pos

Pos返回对象标识符的声明位置.

func (*Nil) String 1.5

func (obj *Nil) String() string

func (*Nil) Type 1.5

func (obj *Nil) Type() Type

Type返回对象的类型.

type Object 1.5

对象描述命名的语言实体,例如包,常量,类型,变量,函数(包括方法)或标签. 所有对象都实现对象接口.

type Object interface {
    Parent() *Scope // scope in which this object is declared; nil for methods and struct fields
    Pos() token.Pos // position of object identifier in declaration
    Pkg() *Package  // package to which this object belongs; nil for labels and objects in the Universe scope
    Name() string   // package local object name
    Type() Type     // object type
    Exported() bool // reports whether the name starts with a capital letter
    Id() string     // object name if exported, qualified name if not exported (see func Id)

    // String returns a human-readable string of the object.
    String() string
    // contains filtered or unexported methods
}

func LookupFieldOrMethod 1.5

func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool)

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).

最后一个索引条目是找到条目的(可能是嵌入的)类型的字段或方法索引,可以是:

1) the list of declared methods of a named type; or
2) the list of all methods (method set) of an interface type; or
3) the list of fields of a struct type.

较早的索引条目是从深度0开始遍历以到达找到的条目的嵌入式struct字段的索引.

如果未找到任何条目,则返回nil对象. 在这种情况下,返回的索引和间接值具有以下含义:

	- If index != nil, the index sequence points to an ambiguous entry
	(the same name appeared more than once at the same embedding level).

	- 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.

type Package 1.5

软件包描述了Go软件包.

type Package struct {
    // contains filtered or unexported fields
}

不安全包是进口商为导入路径"不安全"返回的包.

var Unsafe *Package

func NewPackage 1.5

func NewPackage(path, name string) *Package

NewPackage返回给定包路径和名称的新Package. 该软件包不完整,不包含任何显式导入.

func (*Package) Complete 1.5

func (pkg *Package) Complete() bool

如果一个包的作用域包含(至少)所有导出的对象,则该包是完整的; 否则不完整.

func (*Package) Imports 1.5

func (pkg *Package) Imports() []*Package

Imports返回由pkg直接导入的软件包列表; 该列表按源顺序排列.

如果pkg是从导出数据加载的,则Imports包含的软件包将提供pkg引用的软件包级别的对象. 这可能比pkg的源代码直接导入的软件包集更多或更少.

func (*Package) MarkComplete 1.5

func (pkg *Package) MarkComplete()

MarkComplete将包标记为完整.

func (*Package) Name 1.5

func (pkg *Package) Name() string

名称返回包名称.

func (*Package) Path 1.5

func (pkg *Package) Path() string

Path返回包路径.

func (*Package) Scope 1.5

func (pkg *Package) Scope() *Scope

Scope返回(完整或不完整)软件包范围,其中包含在软件包级别声明的对象(TypeNames,Consts,Var和Funcs).

func (*Package) SetImports 1.5

func (pkg *Package) SetImports(list []*Package)

SetImports将明确导入的软件包的列表设置为list. 确保列表元素是唯一的是调用者的责任.

func (*Package) SetName 1.6

func (pkg *Package) SetName(name string)

SetName设置程序包名称.

func (*Package) String 1.5

func (pkg *Package) String() string

type PkgName 1.5

PkgName表示导入的Go软件包. PkgName没有类型.

type PkgName struct {
    // contains filtered or unexported fields
}

func NewPkgName 1.5

func NewPkgName(pos token.Pos, pkg *Package, name string, imported *Package) *PkgName

NewPkgName返回表示导入包的新PkgName对象. 其余参数设置所有对象都有的属性.

func (*PkgName) Exported 1.5

func (obj *PkgName) Exported() bool

导出报告对象是否已导出(以大写字母开头). 它不考虑对象是否在本地(功能)范围内.

func (*PkgName) Id 1.5

func (obj *PkgName) Id() string

Id是Id(obj.Pkg(),obj.Name())的包装.

func (*PkgName) Imported 1.5

func (obj *PkgName) Imported() *Package

Imported返回导入的包. 它与Pkg()不同,后者是包含import语句的程序包.

func (*PkgName) Name 1.5

func (obj *PkgName) Name() string

Name返回对象的名称(本地程序包,不合格).

func (*PkgName) Parent 1.5

func (obj *PkgName) Parent() *Scope

父级返回声明对象的范围. 方法和结构字段的结果为零.

func (*PkgName) Pkg 1.5

func (obj *PkgName) Pkg() *Package

Pkg返回对象所属的包. 对于Universe范围中的标签和对象,结果为nil.

func (*PkgName) Pos 1.5

func (obj *PkgName) Pos() token.Pos

Pos返回对象标识符的声明位置.

func (*PkgName) String 1.5

func (obj *PkgName) String() string

func (*PkgName) Type 1.5

func (obj *PkgName) Type() Type

Type返回对象的类型.

type Pointer 1.5

指针表示指针类型.

type Pointer struct {
    // contains filtered or unexported fields
}

func NewPointer 1.5

func NewPointer(elem Type) *Pointer

NewPointer返回给定元素(基本)类型的新指针类型.

func (*Pointer) Elem 1.5

func (p *Pointer) Elem() Type

Elem返回给定指针p的元素类型.

func (*Pointer) String 1.5

func (p *Pointer) String() string

func (*Pointer) Underlying 1.5

func (p *Pointer) Underlying() Type

type Qualifier 1.5

Qualifier控制在对TypeString,ObjectString和SelectionString的调用中如何打印已命名的包级对象.

这三个格式化例程为每个程序包级对象O调用Qualifier,如果Qualifier返回一个非空字符串p,则该对象以pO的形式打印.如果它返回一个空字符串,则仅打印对象名称O.

使用nil限定符等同于使用(* Package).Path:对象由导入路径限定,例如" encoding / json.Marshal".

type Qualifier func(*Package) string

func RelativeTo 1.5

func RelativeTo(pkg *Package) Qualifier

RelativeTo返回一个限定符,该限定符完全限定pkg以外的所有包的成员.

type Scope 1.5

范围维护一组对象,并链接到其包含(父)范围和包含(子)范围. 可以插入对象并按名称查找. 范围的零值是即用型空范围.

type Scope struct {
    // contains filtered or unexported fields
}

Universe范围包含Go的所有预声明对象. 它是任何嵌套作用域链的最外部作用域.

var Universe *Scope

Example

ExampleScope打印从一组已解析的文件创建的包的作用域树.

package "temperature" scope {
.  const temperature.Boiling temperature.Celsius
.  type temperature.Celsius float64
.  func temperature.FToC(f float64) temperature.Celsius
.  func temperature.Unused()
.  func temperature.main()
.  main.go scope {
.  .  package fmt
.  .  function scope {
.  .  .  var freezing temperature.Celsius
.  .  }
.  }
.  celsius.go scope {
.  .  package fmt
.  .  function scope {
.  .  .  var c temperature.Celsius
.  .  }
.  .  function scope {
.  .  .  var f float64
.  .  }
.  .  function scope {
.  .  .  block scope {
.  .  .  }
.  .  .  block scope {
.  .  .  .  block scope {
.  .  .  .  .  var x int
.  .  .  .  }
.  .  .  }
.  .  }
.  }
}

func NewScope 1.5

func NewScope(parent *Scope, pos, end token.Pos, comment string) *Scope

NewScope返回给定父范围(如果有)中包含的新的空范围. 该注释仅用于调试.

func (*Scope) Child 1.5

func (s *Scope) Child(i int) *Scope

Child返回0 <= i <NumChildren()的第i个子范围.

func (*Scope) Contains 1.5

func (s *Scope) Contains(pos token.Pos) bool

包含报告pos是否在范围之内. 仅当类型检查的AST具有完整的位置信息时,才能保证结果有效.

func (*Scope) End 1.5

func (s *Scope) End() token.Pos

func (*Scope) Innermost 1.5

func (s *Scope) Innermost(pos token.Pos) *Scope

最内层返回包含pos的最内(子)范围. 如果pos不在任何范围内,则结果为nil. 对于Universe范围,结果也为零. 仅当类型检查的AST具有完整的位置信息时,才能保证结果有效.

func (*Scope) Insert 1.5

func (s *Scope) Insert(obj Object) Object

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.

func (*Scope) Len 1.5

func (s *Scope) Len() int

Len返回范围元素的数量.

func (*Scope) Lookup 1.5

func (s *Scope) Lookup(name string) Object

如果存在这样的对象,Lookup将返回范围s中具有给定名称的对象; 否则结果为零.

func (*Scope) LookupParent 1.5

func (s *Scope) LookupParent(name string, pos token.Pos) (*Scope, Object)

LookupParent遵循以s开头的范围的父级链,直到找到其中Lookup(name)返回非nil对象,然后返回该范围和对象的范围. 如果提供了有效位置pos,则仅考虑在pos或之前声明的对象. 如果不存在这样的范围和对象,则结果为(nil,nil).

请注意,如果将对象插入到作用域中并且当时已经有一个父对象,则obj.Parent()可能与返回的作用域不同(请参见下面的插入). 这仅适用于点导入对象,其范围是导出它们的包的范围.

func (*Scope) Names 1.5

func (s *Scope) Names() []string

名称按排序顺序返回范围的元素名称.

func (*Scope) NumChildren 1.5

func (s *Scope) NumChildren() int

NumChildren返回嵌套在s中的范围数.

func (*Scope) Parent 1.5

func (s *Scope) Parent() *Scope

父级返回范围的包含(父级)范围.

func (*Scope) Pos 1.5

func (s *Scope) Pos() token.Pos

Pos和End描述了范围的源代码范围[pos,end). 仅当类型检查的AST具有完整的位置信息时,才能保证结果有效. 对于Universe和包范围,范围未定义.

func (*Scope) String 1.5

func (s *Scope) String() string

String返回范围的字符串表示形式,以进行调试.

func (*Scope) WriteTo 1.5

func (s *Scope) WriteTo(w io.Writer, n int, recurse bool)

WriteTo将范围的字符串表示形式写入w,范围元素按名称排序. 压痕级别由n> = 0控制,其中n == 0表示无压痕. 如果设置了递归,它还将写入嵌套(子级)作用域.

type Selection 1.5

选择描述了选择器表达式xf对于声明:

type T struct{ x int; E }
type E struct{}
func (e E) m() {}
var p *T

存在以下关系:

Selector    Kind          Recv    Obj    Type               Index     Indirect

p.x         FieldVal      T       x      int                {0}       true
p.m         MethodVal     *T      m      func (e *T) m()    {1, 0}    true
T.m         MethodExpr    T       m      func m(_ T)        {1, 0}    false
type Selection struct {
    // contains filtered or unexported fields
}

func (*Selection) Index 1.5

func (s *Selection) Index() []int

索引描述xf中从x到f的路径.最后一个索引条目是声明f的类型的字段或方法索引. 要么:

1) the list of declared methods of a named type; or
2) the list of methods of an interface type; or
3) the list of fields of a struct type.

较早的索引条目是从嵌入深度0开始隐式遍历以从(x的类型)到f的嵌入字段的索引.

func (*Selection) Indirect 1.5

func (s *Selection) Indirect() bool

间接报告在xf中从x到f是否需要任何指针间接调用

func (*Selection) Kind 1.5

func (s *Selection) Kind() SelectionKind

种类返回选择种类.

func (*Selection) Obj 1.5

func (s *Selection) Obj() Object

Obj返回由xf表示的对象; * Var用于字段选择,* Func用于所有其他情况.

func (*Selection) Recv 1.5

func (s *Selection) Recv() Type

Recv returns the type of x in x.f.

func (*Selection) String 1.5

func (s *Selection) String() string

func (*Selection) Type 1.5

func (s *Selection) Type() Type

Type返回xf的类型,该类型可能与f的类型不同. 有关更多信息,请参见选择.

type SelectionKind 1.5

SelectionKind描述选择器表达式xf的类型(不包括合格标识符).

type SelectionKind int
const (
    FieldVal   SelectionKind = iota // x.f is a struct field selector
    MethodVal                       // x.f is a method selector
    MethodExpr                      // x.f is a method expression
)

type Signature 1.5

签名表示(非内置)函数或方法类型. 比较身份签名时,将忽略接收方.

type Signature struct {
    // contains filtered or unexported fields
}

func NewSignature 1.5

func NewSignature(recv *Var, params, results *Tuple, variadic bool) *Signature

NewSignature为给定的接收器,参数和结果返回一个新的函数类型,其中任何一个都不为零. 如果设置了可变参数,则该函数为可变参数,它必须至少具有一个参数,并且最后一个参数必须为未命名切片类型.

func (*Signature) Params 1.5

func (s *Signature) Params() *Tuple

参数返回签名s的参数或nil.

func (*Signature) Recv 1.5

func (s *Signature) Recv() *Var

Recv返回签名s的接收者(如果是方法),或者返回nil(如果是函数). 在比较签名的身份时将忽略它.

对于抽象方法,Recv以* Named或* Interface返回封闭的接口. 由于嵌入,接口可能包含其接收者类型为不同接口的方法.

func (*Signature) Results 1.5

func (s *Signature) Results() *Tuple

结果返回签名s的结果,即nil.

func (*Signature) String 1.5

func (s *Signature) String() string

func (*Signature) Underlying 1.5

func (s *Signature) Underlying() Type

func (*Signature) Variadic 1.5

func (s *Signature) Variadic() bool

可变参数报告签名s是否可变.

type Sizes 1.5

大小定义了不安全包装的大小调整功能.

type Sizes interface {
    // Alignof returns the alignment of a variable of type T.
    // Alignof must implement the alignment guarantees required by the spec.
    Alignof(T Type) int64

    // Offsetsof returns the offsets of the given struct fields, in bytes.
    // Offsetsof must implement the offset guarantees required by the spec.
    Offsetsof(fields []*Var) []int64

    // Sizeof returns the size of a variable of type T.
    // Sizeof must implement the size guarantees required by the spec.
    Sizeof(T Type) int64
}

func SizesFor 1.9

func SizesFor(compiler, arch string) Sizes

SizesFor返回编译器用于体系结构的Sizes. 如果编译器/体系结构对未知,则结果为nil.

编译器" gc"支持的体系结构:" 386"," arm"," arm64"," amd64"," amd64p32"," mips"," mipsle"," mips64"," mips64le"," ppc64"," ppc64le" "," riscv64"," s390x"," sparc64"," wasm".

type Slice 1.5

切片表示切片类型.

type Slice struct {
    // contains filtered or unexported fields
}

func NewSlice 1.5

func NewSlice(elem Type) *Slice

NewSlice返回给定元素类型的新切片类型.

func (*Slice) Elem 1.5

func (s *Slice) Elem() Type

Elem返回slice的元素类型.

func (*Slice) String 1.5

func (s *Slice) String() string

func (*Slice) Underlying 1.5

func (s *Slice) Underlying() Type

type StdSizes 1.5

StdSizes是用于创建常用大小的一种便利类型. 它做出以下简化的假设:

	- The size of explicitly sized basic types (int16, etc.) is the
	  specified size.
	- The size of strings and interfaces is 2*WordSize.
	- The size of slices is 3*WordSize.
	- 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.
     - 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.
	- Arrays and structs are aligned per spec definition; all other
	  types are naturally aligned with a maximum alignment MaxAlign.

* StdSizes实现Sizes.

type StdSizes struct {
    WordSize int64 // word size in bytes - must be >= 4 (32bits)
    MaxAlign int64 // maximum alignment in bytes - must be >= 1
}

func (*StdSizes) Alignof 1.5

func (s *StdSizes) Alignof(T Type) int64

func (*StdSizes) Offsetsof 1.5

func (s *StdSizes) Offsetsof(fields []*Var) []int64

func (*StdSizes) Sizeof 1.5

func (s *StdSizes) Sizeof(T Type) int64

type Struct 1.5

Struct表示结构类型.

type Struct struct {
    // contains filtered or unexported fields
}

func NewStruct 1.5

func NewStruct(fields []*Var, tags []string) *Struct

NewStruct返回具有给定字段和相应字段标签的新结构. 如果索引为i的字段具有标签,则tag [i]必须是该标签,但是len(tags)的长度可能只有保存最大索引i的标签所需的时间. 因此,如果没有字段具有标签,则标签可能为零.

func (*Struct) Field 1.5

func (s *Struct) Field(i int) *Var

字段返回第0个i = N <NumFields()的第i个字段.

func (*Struct) NumFields 1.5

func (s *Struct) NumFields() int

NumFields返回结构中的字段数(包括空白字段和​​嵌入字段).

func (*Struct) String 1.5

func (s *Struct) String() string

func (*Struct) Tag 1.5

func (s *Struct) Tag(i int) string

标记返回第0个i = N <NumFields()的第i个字段标记.

func (*Struct) Underlying 1.5

func (s *Struct) Underlying() Type

type Tuple 1.5

元组表示变量的有序列表; nil *元组是有效的(空)元组. 元组用作签名的组成部分,并表示多个分配的类型; 他们不是围棋的头等舱类型.

type Tuple struct {
    // contains filtered or unexported fields
}

func NewTuple 1.5

func NewTuple(x ...*Var) *Tuple

NewTuple返回给定变量的新元组.

func (*Tuple) At 1.5

func (t *Tuple) At(i int) *Var

At返回元组t的第i个变量.

func (*Tuple) Len 1.5

func (t *Tuple) Len() int

Len返回元组t的数字变量.

func (*Tuple) String 1.5

func (t *Tuple) String() string

func (*Tuple) Underlying 1.5

func (t *Tuple) Underlying() Type

type Type 1.5

类型表示Go的类型. 所有类型都实现Type接口.

type Type interface {
    // Underlying returns the underlying type of a type.
    Underlying() Type

    // String returns a string representation of a type.
    String() string
}

func Default 1.8

func Default(typ Type) Type

Default为"无类型"类型返回默认的"有类型"类型; 它返回所有其他类型的传入类型. 无类型nil的默认类型为无类型nil.

type TypeAndValue 1.5

TypeAndValue报告相应表达式的类型和值(用于常量).

type TypeAndValue struct {
    Type  Type
    Value constant.Value
    // contains filtered or unexported fields
}

func Eval 1.5

func Eval(fset *token.FileSet, pkg *Package, pos token.Pos, expr string) (_ TypeAndValue, err error)

Eval返回类型,如果为常数,则返回在包pkg位置pos处计算的expr表达式的值,该值必须是通过对AST进行类型检查并使用相对于所提供文件集的完整位置信息得出的.

参数fset,pkg和pos的含义与CheckExpr中的相同. 如果无法成功解析expr,或者无法对生成的expr AST进行类型检查,则返回错误.

func (TypeAndValue) Addressable 1.5

func (tv TypeAndValue) Addressable() bool

可寻址报告了相应的表达式是否可寻址( https://golang.org/ref/spec#Address_operators ).

func (TypeAndValue) Assignable 1.5

func (tv TypeAndValue) Assignable() bool

可分配报告相应的表达式是否可分配给(提供正确类型的值).

func (TypeAndValue) HasOk 1.5

func (tv TypeAndValue) HasOk() bool

HasOk报告是否可以在逗号分隔分配的rhs上使用相应的表达式.

func (TypeAndValue) IsBuiltin 1.5

func (tv TypeAndValue) IsBuiltin() bool

IsBuiltin报告相应的表达式是否表示一个(可能带有括号的)内置函数.

func (TypeAndValue) IsNil 1.5

func (tv TypeAndValue) IsNil() bool

IsNil报告相应的表达式是否表示预声明的值nil.

func (TypeAndValue) IsType 1.5

func (tv TypeAndValue) IsType() bool

IsType报告相应的表达式是否指定类型.

func (TypeAndValue) IsValue 1.5

func (tv TypeAndValue) IsValue() bool

IsValue报告相应的表达式是否为值. 内置不被视为值. 常数值为非零值.

func (TypeAndValue) IsVoid 1.5

func (tv TypeAndValue) IsVoid() bool

IsVoid报告相应的表达式是否为没有结果的函数调用.

type TypeName 1.5

TypeName表示(定义或别名)类型的名称.

type TypeName struct {
    // contains filtered or unexported fields
}

func NewTypeName 1.5

func NewTypeName(pos token.Pos, pkg *Package, name string, typ Type) *TypeName

NewTypeName返回表示给定类型的新类型名称. 其余参数设置所有对象都有的属性.

typ参数可以是已定义(命名)类型或别名类型. 也可能为nil,这样返回的TypeName可以用作NewNamed的参数,这会将TypeName的类型设置为副作用.

func (*TypeName) Exported 1.5

func (obj *TypeName) Exported() bool

导出报告对象是否已导出(以大写字母开头). 它不考虑对象是否在本地(功能)范围内.

func (*TypeName) Id 1.5

func (obj *TypeName) Id() string

Id是Id(obj.Pkg(),obj.Name())的包装.

func (*TypeName) IsAlias 1.9

func (obj *TypeName) IsAlias() bool

IsAlias报告obj是否是类型的别名.

func (*TypeName) Name 1.5

func (obj *TypeName) Name() string

Name返回对象的名称(本地程序包,不合格).

func (*TypeName) Parent 1.5

func (obj *TypeName) Parent() *Scope

父级返回声明对象的范围. 方法和结构字段的结果为零.

func (*TypeName) Pkg 1.5

func (obj *TypeName) Pkg() *Package

Pkg返回对象所属的包. 对于Universe范围中的标签和对象,结果为nil.

func (*TypeName) Pos 1.5

func (obj *TypeName) Pos() token.Pos

Pos返回对象标识符的声明位置.

func (*TypeName) String 1.5

func (obj *TypeName) String() string

func (*TypeName) Type 1.5

func (obj *TypeName) Type() Type

Type返回对象的类型.

type Var 1.5

变量表示已声明的变量(包括函数参数和结果以及结构字段).

type Var struct {
    // contains filtered or unexported fields
}

func NewField 1.5

func NewField(pos token.Pos, pkg *Package, name string, typ Type, embedded bool) *Var

NewField返回代表结构字段的新变量. 对于嵌入式字段,名称是可访问该字段的非限定类型名称/.

func NewParam 1.5

func NewParam(pos token.Pos, pkg *Package, name string, typ Type) *Var

NewParam返回代表函数参数的新变量.

func NewVar 1.5

func NewVar(pos token.Pos, pkg *Package, name string, typ Type) *Var

NewVar返回一个新变量. 这些参数设置所有对象都有的属性.

func (*Var) Anonymous 1.5

func (obj *Var) Anonymous() bool

匿名报告变量是否为嵌入式字段. 与嵌入式相同; 仅为了向后兼容而存在.

func (*Var) Embedded 1.11

func (obj *Var) Embedded() bool

嵌入式报告变量是否为嵌入式字段.

func (*Var) Exported 1.5

func (obj *Var) Exported() bool

导出报告对象是否已导出(以大写字母开头). 它不考虑对象是否在本地(功能)范围内.

func (*Var) Id 1.5

func (obj *Var) Id() string

Id是Id(obj.Pkg(),obj.Name())的包装.

func (*Var) IsField 1.5

func (obj *Var) IsField() bool

IsField报告变量是否为结构字段.

func (*Var) Name 1.5

func (obj *Var) Name() string

Name返回对象的名称(本地程序包,不合格).

func (*Var) Parent 1.5

func (obj *Var) Parent() *Scope

父级返回声明对象的范围. 方法和结构字段的结果为零.

func (*Var) Pkg 1.5

func (obj *Var) Pkg() *Package

Pkg返回对象所属的包. 对于Universe范围中的标签和对象,结果为nil.

func (*Var) Pos 1.5

func (obj *Var) Pos() token.Pos

Pos返回对象标识符的声明位置.

func (*Var) String 1.5

func (obj *Var) String() string

func (*Var) Type 1.5

func (obj *Var) Type() Type

Type返回对象的类型.

by  ICOPY.SITE