Title Loading...

  1. Time Loading...
  2. Wordcount Loading...
  3. Pluto

Catalogue

Golang 学习笔记 - (二) 常用命令

命令行输入 go help 查看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         download and install packages and dependencies
        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 build program.go

//该命令用于编译并运行程序
go run program.go

//该命令会将代码格式化并输出到终端
gofmt program.go

//该命令会将代码格式化并覆盖源文件
gofmt -w program.go

//该命令会格式化并重写 folder 目录及其子目录下的所有 Go 源文件
gofmt folder

//该命令获取包的文档注释
go doc package

//该命令用于获取子包的文档注释
go doc package/subpackage

//该命令用于获取某个函数在某个包中的文档注释
go doc package function

//该命令用于编译并安装指定的代码包及它们的依赖包
go install

//该命令用于根据要求和实际情况从互联网上下载或更新指定的代码败一级依赖包,并对他们进行编译和安装
go get

//该命令用于自动读取源码目录下面名为 *_test.go的文件,生成并运行测试用的可执行文件
go test