1. 目录结构
GOPATHsrcpkgbin

首先,不管是什么系统或项目,目录的存在肯定是为了使系统或项目的结构更加清晰、更加方便管理,在Go这里也不例外。

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

src
pkg
bin
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]
commandgo help 
Use "go help <command>" for more information about a command.
installgo help install
topicgo helo 
Use "go help <topic>" for more information about that topic.
go help
go buildgo rungo getgo install

2.2. 准备代码

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

src
+- 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目录下)。

go build
go build
go build
go build
go build github.comxingrenguanxuemain
mainmain.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 buildGOROOTsrcGOPATHsrcmainsrcgithub.comxingrenguanxuegithub.comxingrenguanxue
go build
cd github.comxingrenguanxuemain
go build
go build
go build
go build github.comxingrenguanxuehello
mainmainmain
go build
main.go
main
cd github.comxingrenguanxuemain

编译:

go build main.go
go run
go buildgo run
main
cd .github.comxingrenguanxuemain
go run
go run main.go

可以看到输出了语句。

go runmainmainmain
hellogo runhello.go
go run hello.go
go run: cannot run non-main package
go install
GOPATH/bingo buildGOPATH/bingo installgo buildGOPATH/bin
go install github.comxingrenguanxuemain
GOPATH/binmain.exe

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

GOPATHbinPath
go get
GOPATH
gitgit
go get -u github.com/gin-gonic/gin
xingrenguanxuehello-world
+- hello-world
    +- main.go
    +- test
        +- test.go
hello-world
go get github.com/xingrenguanxue/hello-world
GOPATHhello-worldGOPATHbinhello-world.exe

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

go get -u github.com/xingrenguanxue/hello-world
go help
作者简介

如有错误,还请指正。

  • 还没有人评论,欢迎说说您的想法!