GO工具链简易入门-go list

快速上手

我们可以通过go list xx来使用go list.该指令会返回xx package的规范名字

% cd $GOPATH/src/code.google.com/p/go.crypto/ssh
% go list
code.google.com/p/go.crypto/ssh
% go list github.com/juju/juju
github.com/juju/juju

但是go list的要点在于-f选项.该选项允许我们按照指定格式输出我们关心的内容.

事实上,使用go list xx等价于go list -f '{{ .ImportPath }}' xx

一些常用指令

go list的具体用法如下

go list [-f format] [-json] [-m] [list flags] [build flags] [packages]

其中-f中包含了大量的可配置选项,具体可以看这里, 我们在这里会简单的列举一些常用的功能

查找编译会涉及到的所有文件

通常用于条件编译中检查有哪些文件会被分别编译

  • 使用方法

    go list -f '{{ .GoFiles }}' xx

  • 实例

    % env GOOS=darwin go list -f '{{ .GoFiles }}' github.com/pkg/term
    [term.go term_bsd.go]
    % env GOOS=linux go list -f '{{ .GoFiles }}' github.com/pkg/term
    [term.go term_linux.go]
    

查找直接依赖以及所有依赖(包括间接)

  • 使用方法

    • 检查所有直接依赖

      go list -f '{{ .Imports }}' xxx

    • 检查所有直接和间接依赖

      go list -f '{{ .Deps }}' xxx

  • 实例

    % go list -f '{{ .Imports }}' github.com/davecheney/profile
    [io/ioutil log os os/signal path/filepath runtime runtime/pprof]
    % go list -f '{{ .Deps }}' github.com/davecheney/profile
    [bufio bytes errors fmt io io/ioutil log math os os/signal path/filepath reflect runtime runtime/pprof sort strconv strings sync sync/atomic syscall text/tabwriter time unicode unicode/utf8 unsafe]
    

使用模板函数(template function)

go list使用了template,因此我们可以通过通过内置的模板函数,具体来说有三个:join(相当于strigns.Join), context(返回Context结构体), module

以join为例,我们可以用join来简单实现换行的效果

  • 使用方法

    go list -f '{{ join .Imports "\n" }}' xx

  • 实例

    % go list -f '{{ join .Imports "\n" }}' github.com/davecheney/profile
    io/ioutil
    log
    os
    os/signal
    path/filepath
    runtime
    runtime/pprof
    

模块版本下

-m选项会让go list列举模块而不是包(package),此时-f使用的是Module结构体

列举当前模块的规范名字, 版本和可取代物(replacement)
  • 使用方法

    go list -m

  • 实例

    % go list -m 
    practice
    
列举当前模块及其所有依赖的规范名字, 版本和可取代物
  • 使用方法

    go list -m all

    注意这里的all会识别包含当前目录的模块及其依赖

  • 实例

    % go list -m all
    my/main/module
    golang.org/x/text v0.3.0 => /tmp/text
    rsc.io/pdf v0.1.1
    
列举当前模块的依赖可用更新
  • 使用方法

    go list -m -u all

  • 实例

    % go list -m -u all
    practice
    github.com/PuerkitoBio/goquery v1.5.1
    github.com/andybalholm/cascadia v1.1.0
    github.com/urfave/negroni v1.0.0
    golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
    golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e
    golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd
    golang.org/x/text v0.3.0
    
  • 附注

    可以使用go list -m -u -json all来方便解析

FAQ

模板函数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
    BuildTags     []string // build constraints to match in +build lines
    ReleaseTags   []string // releases the current release is compatible with
    InstallSuffix string   // suffix to use in the name of the install dir
}

模板函数module解释

module函数接受一个string作为模块名字, 返回Module结构体. 如果发生错误,那么返回一个非nil的错误

Module结构体如下
type Module struct {
    Path      string       // module path
    Version   string       // module version
    Versions  []string     // available module versions (with -versions)
    Replace   *Module      // replaced by this module
    Time      *time.Time   // time version was created
    Update    *Module      // available update, if any (with -u)
    Main      bool         // is this the main module?
    Indirect  bool         // is this module only an indirect dependency of main module?
    Dir       string       // directory holding files for this module, if any
    GoMod     string       // path to go.mod file used when loading this module, if any
    GoVersion string       // go version used in module
    Error     *ModuleError // error loading module
}

type ModuleError struct {
    Err string // the error itself
}

结论

go list是一个可以提供非常多信息的命令行参数, 利用该工具我们可以更好地写脚本

参考

https://dave.cheney.net/2014/09/14/go-list-your-swiss-army-knife

https://golang.org/cmd/go/#hdr-List_packages_or_modules

发布了31 篇原创文章 · 获赞 32 · 访问量 723

猜你喜欢

转载自blog.csdn.net/a348752377/article/details/105583893