preamble
golang Quick Start [2.1] -go-language development environment configuration -windows
golang Quick Start [2.2] -go-language development environment configuration -macOS
golang Quick Start [2.3] -go-language development environment configuration -linux
golang Quick Start [4] -go language how to compile to machine code
golang Quick Start [5.1] How -go language is running - Linker
golang Quick Start [5.2] How -go language is running - Memory Overview
golang Quick Start [5.3] How -go language is running - memory allocation
golang Quick Start [6.1] - Detailed Integrated Development Environment -goland
golang Quick Start [6.2] - Detailed Integrated Development Environment -emacs
Foreword
In the previous article, we introduced the configuration go language development environment
In this chapter, we will introduce the project structure go language, project management and dependency management. In this article, we focus
gopath
What is gopath
In the go-language development environment configuration article, we introduced the configuration
gopath
andgoroot
the steps environment variables, but not its in-depth explanation. You may be in a terminalgo env
orgo env gopath
to view a particular configuration
C:\Windows\system32> go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\jackson\AppData\Local\go-build
set GOENV=C:\Users\jackson\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\jackson\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=c:\go
...
After go1.8, if you do not specify gopath, the gopath is the default.
In mac, linux is
$HOME/go
windows next to
%USERPROFILE%\g
gopath
It can be understood as the working language of space go inside the storesrc
,bin
,pkg
three folders
go/
├── bin
├── pkg
└── src
$GOPATH/bin
Directory stores bygo install
binaries installed. The operating system usesPATH variable$GOPATH/pkg
Directory stores precompiled obj file (file names based on different operating systems and different, such as under macdarwin_amd64
) to speed up the follow-up to the compiler. Most developers do not need to access this directory. Will be introduced later, mod files will be stored under pkggo module
dependence.$GOPATH/src
Directory to store our project go code. Usually contain many version control repository (for example, managed by Git), each repository contains one or more of the package, each package contains one or more of the Go source files in a directory.Thus, the entire path looks like:
go/
├── bin
└── main.exe
├── pkg
├── darwin_amd64
└── mod
└── src
├── github.com
│ ├── tylfin
│ │ ├── dynatomic
│ │ └── geospy
│ └── uudashr
│ └── gopkgs
└── golang.org
└── x
└── tools
gopath have more effect, when we want to go get the code from github project or elsewhere, we can use the
go get
command. At this point the program will default to the code stored in the$GOPATH/src
directory. Pulling examplego get github.com/dreamerjackson/theWayToGolang
, the directory structure is as follows:
go/
├── bin
├── pkg
└── src
└── github.com
└── dreamerjackson
└── theWayToGolang
When we use
go get
the-u
parameter, and all other items will be the project depends to a batch download$GOPATH/src
directoryAnother feature gopath is clear package import position. We previously introduced, go through the organization code package, in helloworl program, we import go language built-in
fmt
package. When we want to import third party should be how to do it? In fact, if we import the project as a third-party packages, such as
import "blue/red"
The actual quote is the $GOPATH/src/blue/red
code from a file.
Similarly, if imported as
import "github.com/gobuffalo/buffalo"
The actual quote is the $GOPATH/src/github.com/gobuffalo/buffalo
code from a file.
Here we use an example to illustrate the package of the third party
First,
$GOPATH/src
create a new foldermymath
, create a new file in the folderadd.go
» mkdir mymath
» cd mymath
» touch add.go
add.go reads as follows, should pay attention to the exported functions must be capitalized, which is the language of the rules go.
package mymath
func Add(a int, b int) int {
return a + b
}
Then in $GOPATH/src
creating a main.go file, which can call a complete mymath
package of add
functions.
package main
import (
"fmt"
"mymath"
)
func main() {
result := mymath.Add(1, 2)
fmt.Println("result:", result)
}
By
go run main.go
to run output1+2
results3
gopath merits
Compared to other tedious configuration language, go language workspace
gopath
configuration is relatively simple, easy to understandgopath so that the file system to organize the entire code is more concise, structured, but limited to a single workspace.
gopath并没有解决版本依赖的问题,而将其留给了其他工具去实现。正因为如此,gopath中的代码就是一个唯一的master分支,并且强制使用各个模块最新的代码。
总结
本文介绍了gopath的含义、功能、优劣、以及如何通过GOPATH来组织项目,导入第三方库。
在go1.13之后,go官方已经开始全面拥抱
go module
.我们在下文中,将介绍go module
的原理和用法,以及如何通过go module
进行go语言的依赖管理与项目组织。