Golang基本命令详解

原文源码等更多内容可参见:

go --help

C:\Users\zhang>go --help
Go is a tool for managing Go source code.
Usage:
      go <command> [arguments]
      其中 command 是操作命令 arg是该命令的参数,像:go run hello.go
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:
      buildconstraint build constraints
      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.

C:\Users\zhang>

go build

go build compile packages and dependencies
  :编译包和依赖项,可检查是否存在编译错误,如果被编译的是main包,会生成可执行文件;
  :go build hello.go 只编译单个文件成可执行文件;
  :只有文件中有main函数才可以生成可执行文件,否则go build只检查编译错误,不生成可执行文件;
  :可通过 go build 编译带main函数的整个包形成可执行文件
  :例如:工程存放在 C:\Go\src\golang_mainInit_Src\ 目录下
  :在 C:\Go 下打开命令行,敲入go build .\src\golang_mainInit_Src\,则可执行文件生成在C:\Go 目录下;
  :在 C:\Go\src\golang_mainInit_Src\目录下打开命令行,敲入go build,则可执行文件生成在C:\Go\src\golang_mainInit_Src\目录下;
  :注意:使用go build编译整个包,工程要在GOPATH环境变量里,否则系统找不到;
  :go build hello.go则不需要,因为是编译单个文件,具体详情在Golang工作目录及包管理文章中会详细介绍;
  :整个工程包的编译,建议使用IDE;如果是跨平台编译,则可拷贝工程进入其他平台,设置环境变量后,使用go build,后续文章中也会有。

go get

go get add dependencies to current module and install them
  :用于动态获取远程代码包,并将依赖项添加到当前模块并安装它们;
  :go get github.com/mattn/go-sqlite3 默认拉到当前的 GOPATH目录下,可通过go env;
  :github.com/mattn/go-sqlite3 是远程代码包地址,可在浏览器输入浏览;
  :一般都通过包管理工具直接拉取远程包到当前工程目录下,例如:govendor,后续包管理文章会提到;

go run

run compile and run Go program
  :编译和直接运行Go程序,它会生成一个临时文件,但不是一个标准的可执行文件,直接在命令行打印输出程序执行结果,方便用户调试;
  :只能有 main 入口函数的才可以直接通过 go run运行。

go fmt

go fmt 格式化源码,有的IDE保存源码时自动执行该命令,比如subl,也可手动执行它

go install

go install 命令的作用有两步:第一步,编译导入的包文件,所有导入的包文件编译完才会编译主程序;第二步,将编译后生成的可执行文件放到bin目录下(GOPATH/bin),编译后的包文件放到pkg目录下( GOPATH/pkg)

go test

go test命令用于运行测试文件,该命令会自动读取源码目录下的名为:*_test.go的文件,生成并运行测试用的可执行文件,测试成功会显示“PASS”、“OK”等信息。