GoLanggolang 终端命令

Posted 半塘少年

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了GoLanggolang 终端命令相关的知识,希望对你有一定的参考价值。

假如你已安装了golang环境,你可以在命令行执行go命令查看相关的Go语言命令:

go-test-study root$ go
Go is a tool for managing Go source code.

Usage:

        go <command> [arguments]

The commands are:

        bug         start a bug report
        build       compile packages and dependencies
        clean       remove object files and cached files
        doc         show documentation for package or symbol
        env         print Go environment information
        fix         update packages to use new APIs
        fmt         gofmt (reformat) package sources
        generate    generate Go files by processing source
        get         add dependencies to current module and install them
        install     compile and install packages and dependencies
        list        list packages or modules
        mod         module maintenance
        run         compile and run Go program
        test        test packages
        tool        run specified go tool
        version     print Go version
        vet         report likely mistakes in packages

Use "go help <command>" for more information about a command.

Additional help topics:

        buildmode   build modes
        c           calling between Go and C
        cache       build and test caching
        environment environment variables
        filetype    file types
        go.mod      the go.mod file
        gopath      GOPATH environment variable
        gopath-get  legacy GOPATH go get
        goproxy     module proxy protocol
        importpath  import path syntax
        modules     modules, module versions, and more
        module-get  module-aware go get
        module-auth module authentication using go.sum
        module-private module configuration for non-public modules
        packages    package lists and patterns
        testflag    testing flags
        testfunc    testing functions

Use "go help <topic>" for more information about that topic.

指令说明:

  • go env用于打印Go语言的环境信息。
  • go run命令可以编译并运行命令源码文件。
  • go get可以根据要求和实际情况从互联网上下载或更新指定的代码包及其依赖包,并对它们进行编译和安装。
  • go build命令用于编译我们指定的源码文件或代码包以及它们的依赖包。
  • go install用于编译并安装指定的代码包及它们的依赖包。
  • go clean命令会删除掉执行其它命令时产生的一些文件和目录。
  • go doc命令可以打印附于Go语言程序实体上的文档。我们可以通过把程序实体的标识符作为该命令的参数来达到查看其文档的目的。
  • go test命令用于对Go语言编写的程序进行测试。
  • go list命令的作用是列出指定的代码包的信息。
  • go fix会把指定代码包的所有Go语言源码文件中的旧版本代码修正为新版本的代码。
  • go vet是一个用于检查Go语言源码中静态错误的简单工具。
  • go tool pprof命令来交互式的访问概要文件的内容。
  • go fmt 格式化代码

go mod 的包管理

  • go mod init:初始化go mod, 生成go.mod文件,后可接参数指定 module 名,上面已经演示过。
  • go mod download:手动触发下载依赖包到本地cache(默认为$GOPATH/pkg/mod目录)
  • go mod graph: 打印项目的模块依赖结构
  • go mod tidy :添加缺少的包,且删除无用的包
  • go mod verify :校验模块是否被篡改过
  • go mod why: 查看为什么需要依赖
  • go mod vendor :导出项目所有依赖到vendor下
  • go mod edit :编辑go.mod文件,接 -fmt 参数格式化 go.mod 文件,接 -require=golang.org/x/text 添加依赖,接 -droprequire=golang.org/x/text 删除依赖,详情可参考 go help mod edit
  • go list -m -json all:以 json 的方式打印依赖详情
go help mod

-----
Go mod provides access to operations on modules.

Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.

Usage:

        go mod <command> [arguments]

The commands are:

        download    download modules to local cache
        edit        edit go.mod from tools or scripts
        graph       print module requirement graph
        init        initialize new module in current directory
        tidy        add missing and remove unused modules
        vendor      make vendored copy of dependencies
        verify      verify dependencies have expected content
        why         explain why packages or modules are needed

Use "go help mod <command>" for more information about a command.

指令说明

命令说明
downloaddownload modules to local cache(下载依赖包)
editedit go.mod from tools or scripts(编辑go.mod)
graphprint module requirement graph (打印模块依赖图)
initinitialize new module in current directory(在当前目录初始化mod)
tidyadd missing and remove unused modules(拉取缺少的模块,移除不用的模块)
vendormake vendored copy of dependencies(将依赖复制到vendor下)
verifyverify dependencies have expected content (验证依赖是否正确)
whyexplain why packages or modules are needed(解释为什么需要依赖)

golang编译之vendor机制

 Go 1.5引入了vendor 机制,但是需要手动设置环境变量 GO15VENDOREXPERIMENT= 1,Go编译器才能启用。从Go1.6起,,默认开启 vendor 目录查找,vendor 机制就是在包中引入 vendor 目录,将依赖的外部包复制到 vendor 目录下,编译器在查找外部依赖包时,优先在 vendor 目录下查找。整个查找第三方包的流程如下:

  • 如果当前包下有vendor目录,则从其下查找第三方的包,如果没有找到,则继续执行下一步操作。
  • 如果当前包目录下没有vendor目录,则沿当前包目录向上逐级目录查找vendor目录, ,直到找到 $GOPATH/src下的vendor目录,只要找到vendor目录就去其下查找第三方的包,如果没有则继续执行下一步操作。
  • 在GOPATH下面查找依赖包。
  • 在GOROOT目录下面查找依赖包。

优点:vendor将原来放在$GOPATH/src的第三方包放到当前工程的vendor目录中进行管理。它为工程独立的管理自己所依赖第三方包提供了保证,多个工程独立地管理自己的第三方依赖包,它们之间不会相互影响。 vendor将原来包共享模式转换为每个工程独立维护的模式, vendor的另一个好处是保证了工程目录下代码的完整性,将工程代码复制到其他Go编译环境,不需要再去下载第三方包,直接就能编译,这种隔离和解耦的设计思路是一大进步。

缺点: 但vendor也有缺点,那就是对外部依赖的第三方包的版本管理。我们通常使用 go get -u更新第三方包。默认的是将工程的默认分支的最新版本拉取到本地,但并不能指定第三方包的版本。而在实际包升级过程中,如果发现新版本有问题,则不能很快回退,这是个问题。好在Go官方为了解决该问题推出了包依赖管理工具dep。与此同时,社区也有很多包管理工具,比较常用的有godep、govendor、glide。

相关文章

  1. golang 编译之verdor机制
  2. 一文搞懂go moudle 前世今生以及入门使用

以上是关于GoLanggolang 终端命令的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——cli的终端命令大全

VSCode自定义代码片段4——cli的终端命令大全

linux打开终端如何启动scala,如何在终端下运行Scala代码片段?

GoLangGoLang 单元测试性能测试使用方法

GoLanggolang 闭包 closure 参数传递的蹊跷!

GoLanggolang 微服务框架 介绍