24

Golang标准库——go(2)

 3 years ago
source link: https://studygolang.com/articles/31075
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
  • build
  • constant
  • doc

build

包build收集有关Go的信息。

Go Path

Go路径是包含Go源代码的目录树的列表。咨询以解决在标准Go树中找不到的导入。缺省路径是GOPATH环境变量的值,它被解释为适合于操作系统的路径列表(在Unix上,该变量是用冒号分隔的字符串;在Windows上,是用分号分隔的字符串;在Plan 9中,是列表)。

Go路径中列出的每个目录必须具有规定的结构:

src /目录包含源代码。 “ src”下面的路径确定导入路径或可执行文件名称。

pkg /目录包含已安装的软件包对象。就像在Go树中一样,每个目标操作系统和体系结构对都有其自己的pkg子目录(pkg / GOOS_GOARCH)。

如果DIR是Go路径中列出的目录,则可以将源为DIR / src / foo / bar的软件包导入为“ foo / bar”,并将其编译形式安装到“ DIR / pkg / GOOS_GOARCH / foo / bar”。 a”(对于gccgo,则为“ DIR / pkg / gccgo / foo / libbar.a”)。

bin /目录包含已编译的命令。每个命令均以其源目录命名,但仅使用最后一个元素,而不使用整个路径。也就是说,将源代码在DIR / src / foo / quux中的命令安装到DIR / bin / quux中,而不是DIR / bin / foo / quux中。去除了foo /,以便您可以将DIR / bin添加到PATH中以获取安装的命令。

这是示例目录布局:

GOPATH=/home/user/gocode

/home/user/gocode/
    src/
        foo/
            bar/               (go code in package bar)
                x.go
            quux/              (go code in package main)
                y.go
    bin/
        quux                   (installed command)
    pkg/
        linux_amd64/
            foo/
                bar.a          (installed package object)

Build Constraints

构建约束,也称为构建标记,是开始的行注释

// +build

列出了在文件中应包含文件的条件。 约束可能会出现在任何类型的源文件中(不仅是Go),但它们必须出现在文件顶部附近,并且只能出现空白行和其他行注释。 这些规则意味着在Go文件中,构建约束必须出现在package子句之前。

为了将构建约束与软件包文档区分开来,必须在一系列构建约束后跟一个空行。

将构建约束评估为以空格分隔的选项的OR。 每个选项均以其逗号分隔的术语的AND表示; 并且每个术语都是字母数字词,或者以!开头的否定词。 也就是说,构建约束:

// +build linux,386 darwin,!cgo

对应于布尔公式:

(linux AND 386) OR (darwin AND (NOT cgo))

一个文件可能有多个构建约束。 总体约束是各个约束的与。 也就是说,构建约束:

// +build linux darwin
// +build 386

对应于布尔公式:

(linux OR darwin) AND 386

在特定的构建过程中,满足以下条件:

- the target operating system, as spelled by runtime.GOOS
- the target architecture, as spelled by runtime.GOARCH
- the compiler being used, either "gc" or "gccgo"
- "cgo", if ctxt.CgoEnabled is true
- "go1.1", from Go version 1.1 onward
- "go1.2", from Go version 1.2 onward
- "go1.3", from Go version 1.3 onward
- "go1.4", from Go version 1.4 onward
- "go1.5", from Go version 1.5 onward
- "go1.6", from Go version 1.6 onward
- "go1.7", from Go version 1.7 onward
- "go1.8", from Go version 1.8 onward
- any additional words listed in ctxt.BuildTags

如果在删除扩展名和可能的_test后缀后,文件名与以下任何一种模式匹配:

*_GOOS
*_GOARCH
*_GOOS_GOARCH

(例: source_windows_amd64.go)其中GOOS和GOARCH分别表示任何已知的操作系统和体系结构值,则该文件被认为具有要求这些术语的隐式构建约束(除了文件中的任何显式约束之外)。

要避免考虑将文件用于构建:

// +build ignore

(其他任何不满意的词也可以使用,但是“忽略”是常规的。)

要仅在使用cgo且仅在Linux和OS X上构建文件:

// +build linux,cgo darwin,cgo

这样的文件通常与另一个文件配对,该文件实现了其他系统的默认功能,在这种情况下,它将带有约束:

// +build !linux,!darwin !cgo

命名文件dns_windows.go将导致仅在构建Windows软件包时才包含该文件; 同样,仅当为32位x86构建软件包时,才会包括math_386.s。

使用GOOS = android除了匹配android标签和文件外,还像GOOS = linux一样匹配构建标签和文件。

Binary-Only Packages

可以以二进制形式分发软件包,而无需包括用于编译软件包的源代码。 为此,必须使用不包含构建约束排除的源文件分发该软件包,并且该源文件必须包含“ // go:binary-only-package”注释。 就像构建约束一样,此注释必须出现在文件顶部附近,仅在其前面有空白行和其他行注释,并在注释之后带有空白行,以将其与软件包文档分开。 与构建约束不同,此注释仅在非测试Go源文件中被识别。

因此,仅二进制包的最小源代码为:

//go:binary-only-package

package mypkg

源代码可能包括其他Go代码。 该代码永远不会被编译,但是会被godoc之类的工具处理,并且可能对最终用户文档很有用。

Variables

var ToolDir = filepath.Join(runtime.GOROOT(), "pkg/tool/"+runtime.GOOS+"_"+runtime.GOARCH)

ToolDir是包含构建工具的目录。

func ArchChar

func ArchChar(goarch string) (string, error)

ArchChar返回“?” 和一个错误。 在Go的早期版本中,返回的字符串用于派生编译器和链接器工具名称,默认目标文件后缀以及默认链接器输出名称。 从Go 1.5开始,这些字符串不再因体系结构而有所不同。 它们分别是compile,link,.o和a.out。

func IsLocalImport

func IsLocalImport(path string) bool

IsLocalImport报告导入路径是本地导入路径,例如“。”,“ ..”,“ ./ foo”还是“ ../foo”。

type Context

type Context struct {
    GOARCH      string // target architecture
    GOOS        string // target operating system
    GOROOT      string // Go root
    GOPATH      string // Go path
    CgoEnabled  bool   // whether cgo can be used
    UseAllFiles bool   // use files regardless of +build lines, file names
    Compiler    string // compiler to assume when computing target paths

    // The build and release tags specify build constraints
    // that should be considered satisfied when processing +build lines.
    // Clients creating a new context may customize BuildTags, which
    // defaults to empty, but it is usually an error to customize ReleaseTags,
    // which defaults to the list of Go releases the current release is compatible with.
    // In addition to the BuildTags and ReleaseTags, build constraints
    // consider the values of GOARCH and GOOS as satisfied tags.
    BuildTags   []string
    ReleaseTags []string

    // The install suffix specifies a suffix to use in the name of the installation
    // directory. By default it is empty, but custom builds that need to keep
    // their outputs separate can set InstallSuffix to do so. For example, when
    // using the race detector, the go command uses InstallSuffix = "race", so
    // that on a Linux/386 system, packages are written to a directory named
    // "linux_386_race" instead of the usual "linux_386".
    InstallSuffix string

    // JoinPath joins the sequence of path fragments into a single path.
    // If JoinPath is nil, Import uses filepath.Join.
    JoinPath func(elem ...string) string

    // SplitPathList splits the path list into a slice of individual paths.
    // If SplitPathList is nil, Import uses filepath.SplitList.
    SplitPathList func(list string) []string

    // IsAbsPath reports whether path is an absolute path.
    // If IsAbsPath is nil, Import uses filepath.IsAbs.
    IsAbsPath func(path string) bool

    // IsDir reports whether the path names a directory.
    // If IsDir is nil, Import calls os.Stat and uses the result's IsDir method.
    IsDir func(path string) bool

    // HasSubdir reports whether dir is lexically a subdirectory of
    // root, perhaps multiple levels below. It does not try to check
    // whether dir exists.
    // If so, HasSubdir sets rel to a slash-separated path that
    // can be joined to root to produce a path equivalent to dir.
    // If HasSubdir is nil, Import uses an implementation built on
    // filepath.EvalSymlinks.
    HasSubdir func(root, dir string) (rel string, ok bool)

    // ReadDir returns a slice of os.FileInfo, sorted by Name,
    // describing the content of the named directory.
    // If ReadDir is nil, Import uses ioutil.ReadDir.
    ReadDir func(dir string) ([]os.FileInfo, error)

    // OpenFile opens a file (not a directory) for reading.
    // If OpenFile is nil, Import uses os.Open.
    OpenFile func(path string) (io.ReadCloser, error)
}

上下文指定了构建的支持上下文。

var Default Context = defaultContext()

默认是构建的默认上下文。 如果已设置,它将使用GOARCH,GOOS,GOROOT和GOPATH环境变量,否则将使用已编译代码的GOARCH,GOOS和GOROOT。

func (*Context) Import

func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Package, error)

导入返回有关由导入路径命名的Go软件包的详细信息,解释相对于srcDir目录的本地导入路径。 如果该路径是本地导入路径,命名了可以使用标准导入路径导入的软件包,则返回的软件包会将p.ImportPath设置为该路径。

在包含该软件包的目录中,.go,.c,.h和.s文件被视为该软件包的一部分,但以下情况除外:

- .go files in package documentation
- files starting with _ or . (likely editor temporary files)
- files with build constraints not satisfied by the context

如果发生错误,导入将返回非零错误和非零*包含部分信息的包。

func (*Context) ImportDir

func (ctxt *Context) ImportDir(dir string, mode ImportMode) (*Package, error)

ImportDir类似于Import,但是处理在命名目录中找到的Go包。

func (*Context) MatchFile

func (ctxt *Context) MatchFile(dir, name string) (match bool, err error)
MatchFile报告给定目录中具有给定名称的文件是否与上下文匹配,并将包含在该目录的ImportDir创建的Package中。
MatchFile考虑文件的名称,可以使用ctxt.OpenFile读取文件的部分或全部内容。

func (*Context) SrcDirs

func (ctxt *Context) SrcDirs() []string

SrcDirs返回软件包源根目录的列表。 它从当前的Go根目录和Go路径中提取,但是省略了不存在的目录。

type ImportMode

type ImportMode uint

ImportMode控制Import方法的行为。

const (
    // If FindOnly is set, Import stops after locating the directory
    // that should contain the sources for a package. It does not
    // read any files in the directory.
    FindOnly ImportMode = 1 << iota

    // If AllowBinary is set, Import can be satisfied by a compiled
    // package object without corresponding sources.
    //
    // Deprecated:
    // The supported way to create a compiled-only package is to
    // write source code containing a //go:binary-only-package comment at
    // the top of the file. Such a package will be recognized
    // regardless of this flag setting (because it has source code)
    // and will have BinaryOnly set to true in the returned Package.
    AllowBinary

    // If ImportComment is set, parse import comments on package statements.
    // Import returns an error if it finds a comment it cannot understand
    // or finds conflicting comments in multiple source files.
    // See golang.org/s/go14customimport for more information.
    ImportComment

    // By default, Import searches vendor directories
    // that apply in the given source directory before searching
    // the GOROOT and GOPATH roots.
    // If an Import finds and returns a package using a vendor
    // directory, the resulting ImportPath is the complete path
    // to the package, including the path elements leading up
    // to and including "vendor".
    // For example, if Import("y", "x/subdir", 0) finds
    // "x/vendor/y", the returned package's ImportPath is "x/vendor/y",
    // not plain "y".
    // See golang.org/s/go15vendor for more information.
    //
    // Setting IgnoreVendor ignores vendor directories.
    //
    // In contrast to the package's ImportPath,
    // the returned package's Imports, TestImports, and XTestImports
    // are always the exact import paths from the source files:
    // Import makes no attempt to resolve or check those paths.
    IgnoreVendor
)

type MultiplePackageError

type MultiplePackageError struct {
    Dir      string   // directory containing files
    Packages []string // package names found
    Files    []string // corresponding files: Files[i] declares package Packages[i]
}

MultiplePackageError描述了一个目录,其中包含用于多个软件包的多个可构建Go源文件。

func (*MultiplePackageError) Error

func (e *MultiplePackageError) Error() string

type NoGoError

type NoGoError struct {
    Dir string
}

NoGoError是Import用来描述不包含可构建Go源文件的目录的错误。 (它可能仍然包含测试文件,由构建标记隐藏的文件,等等。)

func (*NoGoError) Error

func (e *NoGoError) Error() string

type Package

type Package struct {
    Dir           string   // directory containing package sources
    Name          string   // package name
    ImportComment string   // path in import comment on package statement
    Doc           string   // documentation synopsis
    ImportPath    string   // import path of package ("" if unknown)
    Root          string   // root of Go tree where this package lives
    SrcRoot       string   // package source root directory ("" if unknown)
    PkgRoot       string   // package install root directory ("" if unknown)
    PkgTargetRoot string   // architecture dependent install root directory ("" if unknown)
    BinDir        string   // command install directory ("" if unknown)
    Goroot        bool     // package found in Go root
    PkgObj        string   // installed .a file
    AllTags       []string // tags that can influence file selection in this directory
    ConflictDir   string   // this directory shadows Dir in $GOPATH
    BinaryOnly    bool     // cannot be rebuilt from source (has //go:binary-only-package comment)

    // Source files
    GoFiles        []string // .go source files (excluding CgoFiles, TestGoFiles, XTestGoFiles)
    CgoFiles       []string // .go source files that import "C"
    IgnoredGoFiles []string // .go source files ignored for this build
    InvalidGoFiles []string // .go source files with detected problems (parse error, wrong package name, and so on)
    CFiles         []string // .c source files
    CXXFiles       []string // .cc, .cpp and .cxx source files
    MFiles         []string // .m (Objective-C) source files
    HFiles         []string // .h, .hh, .hpp and .hxx source files
    FFiles         []string // .f, .F, .for and .f90 Fortran source files
    SFiles         []string // .s source files
    SwigFiles      []string // .swig files
    SwigCXXFiles   []string // .swigcxx files
    SysoFiles      []string // .syso system object files to add to archive

    // Cgo directives
    CgoCFLAGS    []string // Cgo CFLAGS directives
    CgoCPPFLAGS  []string // Cgo CPPFLAGS directives
    CgoCXXFLAGS  []string // Cgo CXXFLAGS directives
    CgoFFLAGS    []string // Cgo FFLAGS directives
    CgoLDFLAGS   []string // Cgo LDFLAGS directives
    CgoPkgConfig []string // Cgo pkg-config directives

    // Dependency information
    Imports   []string                    // import paths from GoFiles, CgoFiles
    ImportPos map[string][]token.Position // line information for Imports

    // Test information
    TestGoFiles    []string                    // _test.go files in package
    TestImports    []string                    // import paths from TestGoFiles
    TestImportPos  map[string][]token.Position // line information for TestImports
    XTestGoFiles   []string                    // _test.go files outside package
    XTestImports   []string                    // import paths from XTestGoFiles
    XTestImportPos map[string][]token.Position // line information for XTestImports
}

Package描述了在目录中找到的Go软件包。

func Import

func Import(path, srcDir string, mode ImportMode) (*Package, error)

Import 导入是Default.Import的简写。

func ImportDir

func ImportDir(dir string, mode ImportMode) (*Package, error)

ImportDir是 Default.ImportDir的简写。

func (*Package) IsCommand

func (p *Package) IsCommand() bool

IsCommand报告该包是否被视为要安装的命令(不仅仅是库)。 名为“ main”的包被视为命令。

constant

包常量实现了表示无类型Go常量的值及其对应的操作。

当由于错误而未知的值时,可以使用特殊的未知值。 除非另有说明,否则对未知值进行的运算会产生未知值。

func BitLen

func BitLen(x Value) int

BitLen返回以二进制表示形式表示绝对值x所需的位数。 x必须是Int或Unknown。 如果x为未知,则结果为0。

func BoolVal

func BoolVal(x Value) bool

BoolVal返回x的Go布尔值,该值必须是Bool或Unknown。 如果x为未知,则结果为false。

func Bytes

func Bytes(x Value) []byte

字节以小尾数二进制表示形式返回x的绝对值的字节; x必须是一个Int。

func Compare

func Compare(x_ Value, op token.Token, y_ Value) bool

比较返回比较结果x op y。 必须为操作数定义比较。 如果操作数之一为未知,则结果为false。

func Float32Val

func Float32Val(x Value) (float32, bool)

Float32Val类似于Float64Val,但用于float32而不是float64。

func Float64Val

func Float64Val(x Value) (float64, bool)

Float64Val返回x的最接近的Go float64值以及结果是否准确;否则,返回0。 x必须是数字或未知,但不能是复数。 如果值太小(太接近0)而无法表示为float64,则Float64Val会默默下溢至0。结果符号始终与x的符号匹配,即使对于0也是如此。如果x为Unknown,则结果为(0,false)。

func Int64Val

func Int64Val(x Value) (int64, bool)

Int64Val返回x的Go int64值以及结果是否准确; x必须是Int或Unknown。 如果结果不正确,则其值不确定。 如果x为未知,则结果为(0,false)。

func Sign

func Sign(x Value) int

根据x <0,x == 0或x> 0,符号返回-1、0或1。 x必须为数字或未知。 对于复数值x,如果x == 0,则符号为0,否则为!=0。如果x为Unknown,则结果为1。

func StringVal

func StringVal(x Value) string

StringVal返回x的Go字符串值,该值必须为String或Unknown。 如果x为未知,则结果为“”。

func Uint64Val

func Uint64Val(x Value) (uint64, bool)

Uint64Val返回x的Go uint64值以及结果是否准确;否则,返回0。 x必须是Int或Unknown。 如果结果不正确,则其值不确定。 如果x为未知,则结果为(0,false)。

type Kind

type Kind int

种类指定由值表示的值的种类。

const (
    // unknown values
    Unknown Kind = iota

    // non-numeric values
    Bool
    String

    // numeric values
    Int
    Float
    Complex
)

type Value

type Value interface {
    // Kind returns the value kind.
    Kind() Kind

    // String returns a short, quoted (human-readable) form of the value.
    // For numeric values, the result may be an approximation;
    // for String values the result may be a shortened string.
    // Use ExactString for a string representing a value exactly.
    String() string

    // ExactString returns an exact, quoted (human-readable) form of the value.
    // If the Value is of Kind String, use StringVal to obtain the unquoted string.
    ExactString() string
    // contains filtered or unexported methods
}

Value表示Go常数的值。

func BinaryOp

func BinaryOp(x_ Value, op token.Token, y_ Value) Value

BinaryOp返回二进制表达式x op y的结果。 必须为操作数定义操作。 如果操作数之一为未知,则结果为未知。 BinaryOp不处理比较或移位; 而是使用“比较”或“ Shift”。

要强制对Int操作数进行整数除法,请使用op == token.QUO_ASSIGN代替token.QUO; 在这种情况下,结果保证为Int。 除以零会导致运行时恐慌。

func Denom

func Denom(x Value) Value

Denom返回x的分母; x必须是Int,Float或Unknown。 如果x为未知,或者太大或太小而无法表示为分数,则结果为未知。 否则结果是Int> = 1。

func Imag

func Imag(x Value) Value

Imag返回x的虚部,该虚部必须是数字或未知值。 如果x为未知,则结果为未知。

func MakeBool

func MakeBool(b bool) Value

MakeBool返回b的Bool值。

func MakeFloat64

func MakeFloat64(x float64) Value

MakeFloat64返回x的Float值。 如果x不是有限的,则结果为未知。

func MakeFromBytes

func MakeFromBytes(bytes []byte) Value

给定其Little-endian二进制表示形式的字节,MakeFromBytes返回Int值。 空字节片参数表示0。

func MakeFromLiteral

func MakeFromLiteral(lit string, tok token.Token, zero uint) Value

MakeFromLiteral返回Go文字字符串的相应整数,浮点数,虚数,字符或字符串值。 tok值必须是token.INT,token.FLOAT,token.IMAG,token.CHAR或token.STRING之一。 最后一个参数必须为零。 如果文字字符串语法无效,则结果为未知。

func MakeImag

func MakeImag(x Value) Value

MakeImag返回复杂值x * i; x必须是Int,Float或Unknown。 如果x为未知,则结果为未知。

func MakeInt64

func MakeInt64(x int64) Value

MakeInt64返回x的Int值。

func MakeString

func MakeString(s string) Value

MakeString返回s的String值。

func MakeUint64

func MakeUint64(x uint64) Value

MakeUint64返回x的Int值。

func MakeUnknown

func MakeUnknown() Value

MakeUnknown返回未知值。

func Num

func Num(x Value) Value

Num返回x的分子; x必须是Int,Float或Unknown。 如果x为未知,或者太大或太小而无法表示为分数,则结果为未知。 否则,结果是一个与x具有相同符号的Int。

func Real

func Real(x Value) Value

实数返回x的实数部分,该部分必须是数字或未知值。 如果x为未知,则结果为未知。

func Shift

func Shift(x Value, op token.Token, s uint) Value

Shift返回带有op == token.SHL或token.SHR(<<或>>)的移位表达式x op s的结果。 x必须是Int或Unknown。 如果x为未知,则结果为x。

func ToComplex

func ToComplex(x Value) Value

如果x可表示为Complex,则ToComplex将x转换为Complex值。 否则,它将返回未知。

func ToFloat

func ToFloat(x Value) Value

如果x可表示为Float,则ToFloat会将x转换为Float值。 否则,它将返回未知。

func ToInt

func ToInt(x Value) Value

如果x可表示为Int,则ToInt将x转换为Int值。 否则,它将返回未知。

func UnaryOp

func UnaryOp(op token.Token, y Value, prec uint) Value

UnaryOp返回一元表达式op y的结果。 必须为操作数定义操作。 如果prec> 0,则以位为单位指定^(异或)结果大小。 如果y为未知,则结果为未知。

doc

doc包从Go的AST提取源码文档。

Variables

var IllegalPrefixes = []string{
    "copyright",
    "all rights",
    "author",
}

Examples

func Examples(files ...*ast.File) []*Example

Examples返回在文件中找到的示例,并按“名称”字段排序。 Order字段记录遇到示例的顺序。

可播放示例必须放在名称以“ _test”结尾的程序包中。 在以下两种情况下,示例都是“可播放的”(“播放”字段为非零):

- The example function is self-contained: the function references only
  identifiers from other packages (or predeclared identifiers, such as
  "int") and the test file does not include a dot import.
- The entire test file is the example: the file contains exactly one
  example function, zero test or benchmark functions, and at least one
  top-level function, type, variable, or constant declaration other
  than the example function.

func Synopsis

func Synopsis(s string) string

大纲返回s中第一句话的纯净版本。 该句子在第一个句点后跟空格,而不是紧跟一个大写字母后结束。 结果字符串没有\ n,\ r或\ t字符,并且单词之间仅使用单个空格。 如果s以任何IllegalPrefixes开头,则结果为空字符串。

func ToHTML

func ToHTML(w io.Writer, text string, words map[string]string)

ToHTML将注释文本转换为格式化的HTML。该注释是由DocReader准备的,因此众所周知,它没有前导,尾随的空白行,也没有尾随空格。注释标记已被删除。

无缩进的非空白行的每个跨度都转换为单个段落。该规则有一个例外:一个范围由一行组成,其后是另一个段落范围,以大写字母开头,并且不包含标点符号。

一段缩进的行将转换为<pre>块,并删除公共缩进前缀。

注释文本中的URL转换为链接;如果URL也出现在单词映射中,则从映射中获取链接(如果对应的映射值为空字符串,则URL不会转换为链接)。

单词映射中显示的Go标识符以斜体显示;如果对应的映射值不是空字符串,则将其视为URL并将该单词转换为链接。

func ToText

func ToText(w io.Writer, text string, indent, preIndent string, width int)

ToText准备注释文本以在文本输出中呈现。 它将文本段落包装为宽度或更小的Unicode代码点,然后在每行前面加上缩进。 在预格式化的部分(例如程序文本)中,它在每条非空白行的前面加上preIndent。

type Example

type Example struct {
    Name        string // name of the item being exemplified
    Doc         string // example function doc string
    Code        ast.Node
    Play        *ast.File // a whole program version of the example
    Comments    []*ast.CommentGroup
    Output      string // expected output
    EmptyOutput bool   // expect empty output
    Order       int    // original source code order
}

Example代表在源文件中找到的示例函数。

type Filter

type Filter func(string) bool

type Func

type Func struct {
    Doc  string
    Name string
    Decl *ast.FuncDecl

    // methods
    // (for functions, these fields have the respective zero value)
    Recv  string // actual   receiver "T" or "*T"
    Orig  string // original receiver "T" or "*T"
    Level int    // embedding level; 0 means not embedded
}

Func是func声明的文档。

type Mode

type Mode int

Mode 值控制“新建”的操作。

const (
    // extract documentation for all package-level declarations,
    // not just exported ones
    AllDecls Mode = 1 << iota

    // show all embedded methods, not just the ones of
    // invisible (unexported) anonymous fields
    AllMethods
)

type Note

type Note struct {
    Pos, End token.Pos // position range of the comment containing the marker
    UID      string    // uid found with the marker
    Body     string    // note body text
}

注释表示以“ MARKER(uid):注释正文”开头的标记注释。 识别带有2个或更多大写[A-Z]字母标记和至少一个字符的uid的笔记。 uid后面的“:”是可选的。 便笺收集在Package.Notes映射中,由便笺标记索引。

type Package

type Package struct {
    Doc        string
    Name       string
    ImportPath string
    Imports    []string
    Filenames  []string
    Notes      map[string][]*Note
    // DEPRECATED. For backward compatibility Bugs is still populated,
    // but all new code should use Notes instead.
    Bugs []string

    // declarations
    Consts []*Value
    Types  []*Type
    Vars   []*Value
    Funcs  []*Func
}

包是整个包的文档。

func New

func New(pkg *ast.Package, importPath string, mode Mode) *Package

New计算给定包AST的包文档。 New拥有AST pkg的所有权,并且可以对其进行编辑或覆盖。

func (*Package) Filter

func (p *Package) Filter(f Filter)

过滤器消除了没有通过过滤器f的名称的文档。 TODO(gri):将“ Type.Method”识别为名称。

type Type

type Type struct {
    Doc  string
    Name string
    Decl *ast.GenDecl

    // associated declarations
    Consts  []*Value // sorted list of constants of (mostly) this type
    Vars    []*Value // sorted list of variables of (mostly) this type
    Funcs   []*Func  // sorted list of functions returning this type
    Methods []*Func  // sorted list of methods (including embedded ones) of this type
}

Type是类型声明的文档。

type Value

type Value struct {
    Doc   string
    Names []string // var or const names in declaration order
    Decl  *ast.GenDecl
    // contains filtered or unexported fields
}

值是var或const声明的文档(可能是分组的)。

有疑问加站长微信联系

iiUfA3j.png!mobile

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK