zoukankan      html  css  js  c++  java
  • go packages 学习

    $ go help packages
    Many commands apply to a set of packages:
    许多命令适用于一组包: 
     
        go action [packages]
     
    Usually, [packages] is a list of import paths.
    通常,[packages] 是导入路径的列表。
     
    An import path that is a rooted path or that begins with
    a . or .. element is interpreted as a file system path and
    denotes the package in that directory.
    导入路径,是一个根路径或者以"."或".."元素开始被解释成文件系统路径,以及表示那些目录中的包。(总结一下就是:导入路径包含了文件系统路径以及包名)
     
    Otherwise, the import path P denotes the package found in
    the directory DIR/src/P for some DIR listed in the GOPATH
    environment variable (For more details see: 'go help gopath').
    否则,导入路径P表示在目录DIR/src/P中找到的包,这里的DIR来自GOPATH环境变量中列出的DIR。(总结:在GOPATH包含的目录中寻找导入路径是否存在,如果GOPATH中所有的目录中均未能找到导入路径,则在编译的时候会报错)
     
    If no import paths are given, the action applies to the
    package in the current directory.
    如果没有给出导入路径,则该操作将应用于当前目录中的包。(总结:没有指明导入路径,只有一个包名的话,就匹配当前目录中的包)
     
    There are four reserved names for paths that should not be used
    for packages to be built with the go tool:
    这里保留的四个包名,不应该被用作包名,并使用go工具进行构建。
     
    - "main" denotes the top-level package in a stand-alone executable.
    “main”表示独立可执行文件中的顶级包。
     
    - "all" expands to all package directories found in all the GOPATH
    trees. For example, 'go list all' lists all the packages on the local
    system.
    “all”扩展到在所有GOAPTH目录树中找到的包目录。例如,'go list all' 列出了本地系统中所有的包。
     
    - "std" is like all but expands to just the packages in the standard
    Go library.
    "std" 和all相似,但是扩展的仅仅是保准的GO库中的包。
     
    - "cmd" expands to the Go repository's commands and their
    internal libraries.
    "cmd"扩展GO仓库中的命令和他们内部的库。
     
    Import paths beginning with "cmd/" only match source code in
    the Go repository.
    以"cmd"开头的导入路径仅仅匹配GO仓库中的源代码。
     
    An import path is a pattern if it includes one or more "..." wildcards,
    each of which can match any string, including the empty string and
    strings containing slashes. Such a pattern expands to all package
    directories found in the GOPATH trees with names matching the
    patterns.
    如果导入路径中包含了一个或多个"..."通配符,那导入路径则是一种模式,每一个通配符都能够匹配任何字符串,包括空字符串和包括"/"的字符串。
    这种模式扩展到GOPATH树中所有名称与模式匹配的包目录。
    总结:包含了"..."通配符的模式,会扩展到所有到包目录,在一个项目下执行"go list ./..."只会列出本项目下的所有包,但是如果执行"go list ...",则会列出GOPATH树下所有的包,与"go list all"相同
     
    To make common patterns more convenient, there are two special cases.
    First, /... at the end of the pattern can match an empty string,
    so that net/... matches both net and packages in its subdirectories, like net/http.
    Second, any slash-separated pattern element containing a wildcard never
    participates in a match of the "vendor" element in the path of a vendored
    package, so that ./... does not match packages in subdirectories of
    ./vendor or ./mycode/vendor, but ./vendor/... and ./mycode/vendor/... do.
    Note, however, that a directory named vendor that itself contains code
    is not a vendored package: cmd/vendor would be a command named vendor,
    and the pattern cmd/... matches it.
    See golang.org/s/go15vendor for more about vendoring.
    为了使常用的模式更方便,有两个特殊的例子。
    首先,'/...'在模式的末尾可以匹配一个空字符串,所以'net/...'可以匹配net和它的子目录中的包,像net/http.
    其次,任何斜杠分割的模式元素(模式中的元素包含了通配符,即...)从来不会参与匹配vendor中的包的路径中“vendor”元素的匹配总结:通配符...不会匹配包路径中包含vendor的路径,因此./...不会匹配./vendor或./mycode/vendor子目录中的包,但是./vendor/...和./mycode/vendor/...会匹配。总结:除非模式中指明包含了vendor,否则不会匹配带vendor的目录中的包)。
    但是,请注意,名为vendor的目录本身包含了代码,不是一个被vendor的包:cmd/vendor将是一个命名为vendor的命令,和模式cmd/...匹配。
     
    An import path can also name a package to be downloaded from
    a remote repository. Run 'go help importpath' for details.
    导入路径还能命名一个包,用来从远端仓库中下载。运行'go help importpath'查看详细。
     
    Every package in a program must have a unique import path.
    By convention, this is arranged by starting each path with a
    unique prefix that belongs to you. For example, paths used
    internally at Google all begin with 'google', and paths
    denoting remote repositories begin with the path to the code,
    程序中的每一个包必须拥有一个唯一的导入路径。
    按照管理,通过安排属于你的唯一前缀作为每个路径的起始。例如,google内部使用的路径都以'google'开头,而不是远程仓库都路径都以代码路径开头,例如'github.com/user/repo'。
     
    Packages in a program need not have unique package names,
    but there are two reserved package names with special meaning.
    The name main indicates a command, not a library.
    Commands are built into binaries and cannot be imported.
    The name documentation indicates documentation for
    a non-Go program in the directory. Files in package documentation
    are ignored by the go command.
    程序中的包不必有唯一的包名,但是有两个拥有特殊意义的保留包名。名称main表示一个命令,而不是一个库。命令内置在二进制文件中,不能被导入。
    名称documentation表示目录中非go程序的文档。包文档中的文件被go命令忽略。
     
    As a special case, if the package list is a list of .go files from a
    single directory, the command is applied to a single synthesized
    package made up of exactly those files, ignoring any build constraints
    in those files and ignoring any other files in the directory.
    作为一种特殊情况,如果包列表是来自一个单独目录中的.go文件列表,该命令应用于完全由这些文件组成的一个单独的合成包,忽略任何构建约束在这些文件中,忽略任何其他的文件在这个目录中。
     
    Directory and file names that begin with "." or "_" are ignored
    by the go tool, as are directories named "testdata". 
    以"."或"_"开头的目录和文件名将被go工具忽略,还有名为"testdata"的目录。
     

    总结:
    上述文字讲述了:
    包是什么
    导入路径是什么
    导入路径与GOPATH的关系
    未给出导入路径时命令仅仅应用于当前目录下的包(不会递归遍历子目录)
    四个保留的包名(main、all、std、cmd),以及每个包的意义
    导入路径模式及"..."通配符的意义(请分别讲述go list、go list ./...、go list ...与go list all所表示的意义)
    路径模式中两个特殊例子(主要是net/...不仅会匹配net子目录,同时也会比配目录本身,以及什么时候不能匹配路径中包含vendor的包,什么时候要匹配)
    导入路径与仓库下载
    导入路径的唯一性,及其命名习惯
    main与documentation包的特殊意义
    一个目录(无子目录)中所有的.go文件合成一个单独的包(也就是是一个目录中之多只能包含一个包)
    以"."和"_"开头的目录和文件名将被go工具忽略(在非GOPATH目录下执行go list ./...,会发现所有的导入前面都加上了_)
     
  • 相关阅读:
    hdu4990矩阵快速幂
    预处理+状态压缩+剪枝——codefoece 1209E
    AC自动机处理多串匹配——cf1202E
    二维差分前缀和——cf1202D(好题)
    序列递推——cf1204E(好题)
    建模+线性dp——cf1201D
    暴力——cf1202C
    经典排序背包——cf1203F
    思维+贪心——cf1042D
    分块——cf1207F
  • 原文地址:https://www.cnblogs.com/zcqkk/p/10066402.html
Copyright © 2011-2022 走看看