关于golang:Go语言入门系列Go语言工作目录介绍及命令工具的使用

10次阅读

共计 4740 个字符,预计需要花费 12 分钟才能阅读完成。

【Go 语言入门系列】后面的文章:

  • 【保姆级教程】手把手教你进行 Go 语言环境装置及相干 VSCode 配置
  • 【Go 语言入门系列】(八)Go 语言是不是面向对象语言?
  • 【Go 语言入门系列】(九)写这些就是为了搞懂怎么用接口

1. GOPATH目录构造

在【保姆级教程】手把手教你进行 Go 语言环境装置及相干 VSCode 配置一文中曾经配置过工作空间 GOPATH 的环境变量了,并在工作空间中新建了三个目录 srcpkgbin 了。那为什么要新建这三个目录呢?这三个目录又有什么作用呢?

首先,不论是什么零碎或我的项目,目录的存在必定是为了使零碎或我的项目的构造更加清晰、更加方便管理,在 Go 这里也不例外。

其次,其实只看名字,就能猜个大略:

src目录:即 source,用来寄存源代码。

pkg目录:即 package,用来寄存编译后的文件。

bin目录:即 binary,用来寄存编译后生成的二进制的可执行文件。

因为 Go 是用包来组织代码的,而咱们写代码的时候会援用他人的包,为了防止这些包名的反复,咱们能够应用域名来作为包的前缀。对于集体来说,应用 github 账号更加不便:

+- GOPATH
    +- bin // 寄存编译后的可执行文件
    +- pkg // 寄存编译后的文件
    +- src // 寄存源代码
        +- github.com
        |   +- xingrenguanxue
        |       +- project1
        |       +- project2
        |       +- project3
        +- gitee.com
        +- golang.com

2. Go 的命令工具

2.1. Go

关上命令行终端,输出命令 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 is a tool for managing Go source code.
Go 命令是一个用来治理 Go 的源代码的工具

而后通知了咱们 Go 命令工具的应用形式:

go <command> [arguments]

各种 command 也很贴心地列举了进去,如果你不晓得怎么应用,还能够应用命令 go help <commnd> 查看应用形式:

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

比方,我想理解一些对于 install 命令的信息,则应用命令go help install,即可查看。

还有一些 topic,也能够应用go helo <topic> 命令查看应用形式:

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

所以如果当前对这些命令有纳闷,倡议第一工夫应用 go help 去查看官网是怎么答复的。

上面介绍一下 go buildgo rungo getgo install 这四个命令。

2.2. 筹备代码

写两段代码,用作测试这三个命令的代码。

关上工作空间,上面咱们在 src 目录下写 Go 代码

+- src
    +- github.com
        +- xingrenguanxue
            +- main
            |   +- main.go
            +- hello
                +- hello.go

github.com\xingrenguanxue\main\main.go的代码如下:

package main

import "fmt"

func main() {fmt.Println("你好,世界!Hello,World!")
}

github.com\xingrenguanxue\hello\hello.go的代码如下:

package hello

import "fmt"

func Hello() {fmt.Println("这是 hello 包的 Hello 办法....")
}

应用【Ctrl+`】关上 Terminal 终端,上面的演示用终端进行操作(如上图,当初终端在 GOPATHSRC 目录下)。

2.3. go build

go build命令用来编译包(肯定要指明其导入门路)及其相干的依赖。

(一)go buildmain 包

应用 go build 命令编译包:

go build github.com\xingrenguanxue\main

能够看到会在 main 目录下生成一个可执行文件main.exe,进入该可执行文件所在目录并运行它:

cd github.com\xingrenguanxue\main

.\main

打印出了语句。

留神:当初咱们是在编译包main,如果你是这样进行编译的(未指明导入门路):

go build main

是会报错的:

can't load package: package main: cannot find package"main" in any of:
        C:\Go\src\main (from $GOROOT)
        D:\Work\Program\go\src\main (from $GOPATH)

因为后面咱们曾经配置过环境变量了,go build的时候会从 GOROOT\srcGOPATH\src下找包,而此例中 main 包在 src\github.com\xingrenguanxue\ 目录下,所以如果不带上github.com\xingrenguanxue\,那么必定会找不到的。

当然,你也能够进入该包所在目录,间接应用 go build 命令而不指明包是对以后门路的包进行编译,和下面的办法实质上是一样的:

cd github.com\xingrenguanxue\main
go build

(二)go build一般包

应用 go build 命令编译包:

go build github.com\xingrenguanxue\hello

并没有呈现可执行文件,因为 main 包中 main 函数——程序的入口,所以只有编译 main 包才会呈现可执行文件。

(三)go build文件

如果你想独自编译某个文件,也是能够的。比方当初要编译 main.go 文件:

进入 main 包:

cd github.com\xingrenguanxue\main

编译:

go build main.go

2.4. go run

咱们应用 go build 命令时,须要先编译成可执行文件,而后在运行。而 go run 则将这两步合起来做,并且并不会在目录下生成可执行文件。

进入 main 目录下:

cd .\github.com\xingrenguanxue\main

应用 go run 进行编译:

go run main.go

能够看到输入了语句。

留神:go run命令编译的对象必须包含 main 包下蕴含 main 函数的文件,因为它还是要生成可执行文件(尽管看不到)并帮咱们运行,因而离不开 main 函数。

你可进入 hello 包下,去 go run 一下 hello.go 试一试,会报错:

go run hello.go
go run: cannot run non-main package

2.5. go install

后面介绍过,GOPATH/bin目录下寄存的是咱们编译生成的可执行文件,然而 go build 生成的可执行文件是在源文件目录下。要将其寄存在 GOPATH/bin 目录下就得应用 go install。所以该命令就相当于运行go build,而后将可执行文件挪动到GOPATH/bin 目录下。

go install github.com\xingrenguanxue\main

GOPATH/bin 目录下就能找到 main.exe 文件。

此时你再运行该可执行文件,就不用再进入其所在目录了,可在任意目录下运行该可执行文件

之所以能这样,是因为咱们在配置环境的时候,曾经把 GOPATH\bin 目录退出环境变量 Path 中了。

2.6. go get

该命令用来从网上下载代码到 GOPATH 中,并为咱们编译和装置。

该命令会用到git,所以须要你先下载git

go get -u github.com/gin-gonic/gin

比方我在我的的 GitHub 账号 xingrenguanxue 中有一个名为 hello-world 的仓库,目录构造非常简单:

+- hello-world
    +- main.go
    +- test
        +- test.go

当初应用命令获取该hello-world

go get github.com/xingrenguanxue/hello-world

你能够在 GOPATH 下找到该 hello-world,并且曾经被编译、装置了。(GOPATH\bin 下能够找到可执行文件hello-world.exe)。

如果近程代码库的代码更新了,能够应用以下命令更新本地代码:

go get -u github.com/xingrenguanxue/hello-world

Go 还提供了其余的命令工具,这里不再一一列出,其余命令能够应用 go help 具体查看。

作者简介

【作者】:行小观

【公众号】:行人观学

【简介】:一个面向学习的账号,用乏味的语言写系列文章。包含 Java、Go、数据结构和算法、计算机根底等相干文章。


本文章属于系列文章「Go 语言入门系列」,本系列从 Go 语言根底开始介绍,适宜从零开始的初学者。


欢送关注,咱们一起踏上编程的行程。

如有谬误,还请斧正。

正文完
 0