zoukankan      html  css  js  c++  java
  • 【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.comxingrenguanxuemainmain.go的代码如下:

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

    github.comxingrenguanxuehellohello.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.comxingrenguanxuemain
    

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

    cd github.comxingrenguanxuemain
    
    .main
    

    打印出了语句。

    注意:现在我们是在编译包main,如果你是这样进行编译的(未指明导入路径):

    go build main
    

    是会报错的:

    can't load package: package main: cannot find package "main" in any of:
            C:Gosrcmain (from $GOROOT)
            D:WorkProgramgosrcmain (from $GOPATH)
    

    因为前面我们已经配置过环境变量了,go build的时候会从GOROOTsrcGOPATHsrc下找包,而此例中main包在srcgithub.comxingrenguanxue目录下,所以如果不带上github.comxingrenguanxue,那么肯定会找不到的。

    当然,你也可以进入该包所在目录,直接使用go build命令而不指明包是对当前路径的包进行编译,和上面的方法本质上是一样的:

    cd github.comxingrenguanxuemain
    go build
    

    (二)go build普通包

    使用go build命令编译包:

    go build github.comxingrenguanxuehello
    

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

    (三)go build文件

    如果你想单独编译某个文件,也是可以的。比如现在要编译main.go文件:

    进入main包:

    cd github.comxingrenguanxuemain
    

    编译:

    go build main.go
    

    2.4. go run

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

    进入main目录下:

    cd .github.comxingrenguanxuemain
    

    使用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.comxingrenguanxuemain
    

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

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

    之所以能这样,是因为我们在配置环境的时候,已经把GOPATHin目录加入环境变量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,并且已经被编译、安装了。(GOPATHin下可以找到可执行文件hello-world.exe)。

    如果远程代码库的代码更新了,可以使用以下命令更新本地代码:

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

    Go还提供了其他的命令工具,这里不再一一列出,其他命令可以使用go help具体查看。

    作者简介

    【作者】:行小观

    【公众号】:行人观学

    【简介】:一个面向学习的账号,用有趣的语言写系列文章。包括Java、Go、数据结构和算法、计算机基础等相关文章。


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


    欢迎关注,我们一起踏上编程的行程。

    如有错误,还请指正。

  • 相关阅读:
    072孤荷凌寒从零开始学区块链第72天DAPP027
    014自学方法论_养成随手记录自学过程遇到问题的好习惯
    015把注意力放在养大我们金鹅身上
    071孤荷凌寒从零开始学区块链第71天DAPP026
    070孤荷凌寒从零开始学区块链第70天DAPP024
    068孤荷凌寒从零开始学区块链第68天DAPP022
    014什么才是一个人最宝贵的资产
    013自学方法论_怎样才是最高效率加强记忆的自学
    012自学方法谈_不要依赖视频,培养自己的阅读理解能力
    013学会建立一个个自动化的管道
  • 原文地址:https://www.cnblogs.com/xingrenguanxue/p/13793763.html
Copyright © 2011-2022 走看看