Merging other languages
This commit is contained in:
331
en/01.2.md
331
en/01.2.md
@@ -1,180 +1,151 @@
|
||||
# 1.2 GOPATH与工作空间
|
||||
|
||||
前面我们在安装Go的时候看到需要设置GOPATH变量,Go从1.1版本开始必须设置这个变量,而且不能和Go的安装目录一样,这个目录用来存放Go源码,Go的可运行文件,以及相应的编译之后的包文件。所以这个目录下面有三个子目录:src、bin、pkg
|
||||
|
||||
## GOPATH设置
|
||||
go 命令依赖一个重要的环境变量:$GOPATH
|
||||
|
||||
Windows系统中环境变量的形式为`%GOPATH%`,本书主要使用Unix形式,Windows用户请自行替换。
|
||||
|
||||
*(注:这个不是Go安装目录。下面以笔者的工作目录为示例,如果你想不一样请把GOPATH替换成你的工作目录。)*
|
||||
|
||||
在类似 Unix 环境大概这样设置:
|
||||
```sh
|
||||
export GOPATH=/home/apple/mygo
|
||||
```
|
||||
为了方便,应该新建以上文件夹,并且上一行加入到 `.bashrc` 或者 `.zshrc` 或者自己的 `sh` 的配置文件中。
|
||||
|
||||
Windows 设置如下,新建一个环境变量名称叫做GOPATH:
|
||||
```sh
|
||||
GOPATH=c:\mygo
|
||||
```
|
||||
GOPATH允许多个目录,当有多个目录时,请注意分隔符,多个目录的时候Windows是分号,Linux系统是冒号,当有多个GOPATH时,默认会将go get的内容放在第一个目录下。
|
||||
|
||||
|
||||
以上 $GOPATH 目录约定有三个子目录:
|
||||
|
||||
- src 存放源代码(比如:.go .c .h .s等)
|
||||
- pkg 编译后生成的文件(比如:.a)
|
||||
- bin 编译后生成的可执行文件(为了方便,可以把此目录加入到 $PATH 变量中,如果有多个gopath,那么使用`${GOPATH//://bin:}/bin`添加所有的bin目录)
|
||||
|
||||
以后我所有的例子都是以mygo作为我的gopath目录
|
||||
|
||||
|
||||
## 代码目录结构规划
|
||||
GOPATH下的src目录就是接下来开发程序的主要目录,所有的源码都是放在这个目录下面,那么一般我们的做法就是一个目录一个项目,例如: $GOPATH/src/mymath 表示mymath这个应用包或者可执行应用,这个根据package是main还是其他来决定,main的话就是可执行应用,其他的话就是应用包,这个会在后续详细介绍package。
|
||||
|
||||
|
||||
所以当新建应用或者一个代码包时都是在src目录下新建一个文件夹,文件夹名称一般是代码包名称,当然也允许多级目录,例如在src下面新建了目录$GOPATH/src/github.com/astaxie/beedb 那么这个包路径就是"github.com/astaxie/beedb",包名称是最后一个目录beedb
|
||||
|
||||
下面我就以mymath为例来讲述如何编写应用包,执行如下代码
|
||||
```sh
|
||||
cd $GOPATH/src
|
||||
mkdir mymath
|
||||
```
|
||||
|
||||
新建文件sqrt.go,内容如下
|
||||
```go
|
||||
// $GOPATH/src/mymath/sqrt.go源码如下:
|
||||
package mymath
|
||||
|
||||
func Sqrt(x float64) float64 {
|
||||
z := 0.0
|
||||
for i := 0; i < 1000; i++ {
|
||||
z -= (z*z - x) / (2 * x)
|
||||
}
|
||||
return z
|
||||
}
|
||||
```
|
||||
这样我的应用包目录和代码已经新建完毕,注意:一般建议package的名称和目录名保持一致
|
||||
|
||||
## 编译应用
|
||||
上面我们已经建立了自己的应用包,如何进行编译安装呢?有两种方式可以进行安装
|
||||
|
||||
1、只要进入对应的应用包目录,然后执行`go install`,就可以安装了
|
||||
|
||||
2、在任意的目录执行如下代码`go install mymath`
|
||||
|
||||
安装完之后,我们可以进入如下目录
|
||||
```sh
|
||||
cd $GOPATH/pkg/${GOOS}_${GOARCH}
|
||||
//可以看到如下文件
|
||||
mymath.a
|
||||
```
|
||||
这个.a文件是应用包,那么我们如何进行调用呢?
|
||||
|
||||
接下来我们新建一个应用程序来调用这个应用包
|
||||
|
||||
新建应用包mathapp
|
||||
```sh
|
||||
cd $GOPATH/src
|
||||
mkdir mathapp
|
||||
cd mathapp
|
||||
vim main.go
|
||||
```
|
||||
|
||||
`$GOPATH/src/mathapp/main.go`源码:
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"mymath"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Printf("Hello, world. Sqrt(2) = %v\n", mymath.Sqrt(2))
|
||||
}
|
||||
```
|
||||
|
||||
可以看到这个的package是`main`,import里面调用的包是`mymath`,这个就是相对于`$GOPATH/src`的路径,如果是多级目录,就在import里面引入多级目录,如果你有多个GOPATH,也是一样,Go会自动在多个`$GOPATH/src`中寻找。
|
||||
|
||||
如何编译程序呢?进入该应用目录,然后执行`go build`,那么在该目录下面会生成一个mathapp的可执行文件
|
||||
```sh
|
||||
./mathapp
|
||||
```
|
||||
|
||||
输出如下内容
|
||||
```sh
|
||||
Hello, world. Sqrt(2) = 1.414213562373095
|
||||
```
|
||||
|
||||
如何安装该应用,进入该目录执行`go install`,那么在$GOPATH/bin/下增加了一个可执行文件mathapp, 还记得前面我们把`$GOPATH/bin`加到我们的PATH里面了,这样可以在命令行输入如下命令就可以执行
|
||||
|
||||
```sh
|
||||
mathapp
|
||||
```
|
||||
|
||||
也是输出如下内容
|
||||
|
||||
Hello, world. Sqrt(2) = 1.414213562373095
|
||||
|
||||
这里我们展示如何编译和安装一个可运行的应用,以及如何设计我们的目录结构。
|
||||
|
||||
## 获取远程包
|
||||
go语言有一个获取远程包的工具就是`go get`,目前go get支持多数开源社区(例如:github、googlecode、bitbucket、Launchpad)
|
||||
|
||||
go get github.com/astaxie/beedb
|
||||
|
||||
>go get -u 参数可以自动更新包,而且当go get的时候会自动获取该包依赖的其他第三方包
|
||||
|
||||
通过这个命令可以获取相应的源码,对应的开源平台采用不同的源码控制工具,例如github采用git、googlecode采用hg,所以要想获取这些源码,必须先安装相应的源码控制工具
|
||||
|
||||
通过上面获取的代码在我们本地的源码相应的代码结构如下
|
||||
|
||||
$GOPATH
|
||||
src
|
||||
|--github.com
|
||||
|-astaxie
|
||||
|-beedb
|
||||
pkg
|
||||
|--相应平台
|
||||
|-github.com
|
||||
|--astaxie
|
||||
|beedb.a
|
||||
|
||||
go get本质上可以理解为首先第一步是通过源码工具clone代码到src下面,然后执行`go install`
|
||||
|
||||
在代码中如何使用远程包,很简单的就是和使用本地包一样,只要在开头import相应的路径就可以
|
||||
|
||||
import "github.com/astaxie/beedb"
|
||||
|
||||
## 程序的整体结构
|
||||
通过上面建立的我本地的mygo的目录结构如下所示
|
||||
|
||||
bin/
|
||||
mathapp
|
||||
pkg/
|
||||
平台名/ 如:darwin_amd64、linux_amd64
|
||||
mymath.a
|
||||
github.com/
|
||||
astaxie/
|
||||
beedb.a
|
||||
src/
|
||||
mathapp
|
||||
main.go
|
||||
mymath/
|
||||
sqrt.go
|
||||
github.com/
|
||||
astaxie/
|
||||
beedb/
|
||||
beedb.go
|
||||
util.go
|
||||
|
||||
从上面的结构我们可以很清晰的看到,bin目录下面存的是编译之后可执行的文件,pkg下面存放的是应用包,src下面保存的是应用源代码
|
||||
|
||||
|
||||
## links
|
||||
* [目录](<preface.md>)
|
||||
* 上一节: [GO安装](<01.1.md>)
|
||||
* 下一节: [GO 命令](<01.3.md>)
|
||||
#1.2 $GOPATH and workspace
|
||||
|
||||
## $GOPATH
|
||||
|
||||
Go commands all rely on one important environment variable called $GOPATH. Notice that this is not the $GOROOT variable where Go is installed. This variable points to the workspace of Go on your computer (I use this path on my computer; if you don't have the same directory structure, please replace by yourself).
|
||||
|
||||
In Unix-like systems, the variable should be used like this:
|
||||
|
||||
export GOPATH=/home/apple/mygo
|
||||
|
||||
In Windows, you need to create a new environment variable called GOPATH, then set its value to `c:\mygo`( ***This value depends on where your workspace is located*** )
|
||||
|
||||
It's OK to have more than one path (workspace) in $GOPATH, but remember that you have to use `:`(`;` in Windows) to break them up. At this point, `go get` will save the content to your first path in $GOPATH.
|
||||
|
||||
In $GOPATH, you must have three folders as follows.
|
||||
|
||||
- `src` for source files whose suffix is .go, .c, .g, .s.
|
||||
- `pkg` for compiled files whose suffix is .a.
|
||||
- `bin` for executable files
|
||||
|
||||
In this book, I use `mygo` as my only path in $GOPATH.
|
||||
|
||||
## Package directory
|
||||
|
||||
Create package source files and folders like `$GOPATH/src/mymath/sqrt.go` (`mymath` is the package name) ( ***Author uses `mymath` as his package name, and same name for the folder where contains package source files***)
|
||||
|
||||
Every time you create a package, you should create a new folder in the `src` directory. Folder names are usually the same as the package that you are going to use. You can have multi-level directories if you want to. For example, if you create the directory `$GOPATH/src/github.com/astaxie/beedb`, then the package path would be `github.com/astaxie/beedb`. The package name will be the last directory in your path, which is `beedb` in this case.
|
||||
|
||||
Execute following commands. ( ***Now author goes back to talk examples*** )
|
||||
|
||||
cd $GOPATH/src
|
||||
mkdir mymath
|
||||
|
||||
Create a new file called `sqrt.go`, type following content to your file.
|
||||
|
||||
// Source code of $GOPATH/src/mymath/sqrt.go
|
||||
package mymath
|
||||
|
||||
func Sqrt(x float64) float64 {
|
||||
z := 0.0
|
||||
for i := 0; i < 1000; i++ {
|
||||
z -= (z*z - x) / (2 * x)
|
||||
}
|
||||
return z
|
||||
}
|
||||
|
||||
Now my package directory has been created and its code has been written. I recommend that you use the same name for your packages as their corresponding directories, and that the directories contain all of the package source files.
|
||||
|
||||
## Compile packages
|
||||
|
||||
We've already created our package above, but how do we compile it for practical purposes? There are two ways to do this.
|
||||
|
||||
1. Switch your work path to the directory of your package, then execute the `go install` command.
|
||||
2. Execute the above command except with a file name, like `go install mymath`.
|
||||
|
||||
After compiling, we can open the following folder.
|
||||
|
||||
cd $GOPATH/pkg/${GOOS}_${GOARCH}
|
||||
// you can see the file was generated
|
||||
mymath.a
|
||||
|
||||
The file whose suffix is `.a` is the binary file of our package. How do we use it?
|
||||
|
||||
Obviously, we need to create a new application to use it.
|
||||
|
||||
Create a new application package called `mathapp`.
|
||||
|
||||
cd $GOPATH/src
|
||||
mkdir mathapp
|
||||
cd mathapp
|
||||
vim main.go
|
||||
|
||||
code
|
||||
|
||||
//$GOPATH/src/mathapp/main.go source code.
|
||||
package main
|
||||
|
||||
import (
|
||||
"mymath"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Printf("Hello, world. Sqrt(2) = %v\n", mymath.Sqrt(2))
|
||||
}
|
||||
|
||||
To compile this application, you need to switch to the application directory, which in this case is `$GOPATH/src/mathapp`, then execute the `go install` command. Now you should see an executable file called `mathapp` was generated in the directory `$GOPATH/bin/`. To run this program, use the `./mathapp` command. You should see the following content in your terminal.
|
||||
|
||||
Hello world. Sqrt(2) = 1.414213562373095
|
||||
|
||||
## Install remote packages
|
||||
|
||||
Go has a tool for installing remote packages, which is a command called `go get`. It supports most open source communities, including Github, Google Code, BitBucket, and Launchpad.
|
||||
|
||||
go get github.com/astaxie/beedb
|
||||
|
||||
You can use `go get -u …` to update your remote packages and it will automatically install all the dependent packages as well.
|
||||
|
||||
This tool will use different version control tools for different open source platforms. For example, `git` for Github and `hg` for Google Code. Therefore, you have to install these version control tools before you use `go get`.
|
||||
|
||||
After executing the above commands, the directory structure should look like following.
|
||||
|
||||
$GOPATH
|
||||
src
|
||||
|-github.com
|
||||
|-astaxie
|
||||
|-beedb
|
||||
pkg
|
||||
|--${GOOS}_${GOARCH}
|
||||
|-github.com
|
||||
|-astaxie
|
||||
|-beedb.a
|
||||
|
||||
Actually, `go get` clones source code to the $GOPATH/src of the local file system, then executes `go install`.
|
||||
|
||||
You can use remote packages in the same way that we use local packages.
|
||||
|
||||
import "github.com/astaxie/beedb"
|
||||
|
||||
## Directory complete structure
|
||||
|
||||
If you've followed all of the above steps, your directory structure should now look like the following.
|
||||
|
||||
bin/
|
||||
mathapp
|
||||
pkg/
|
||||
${GOOS}_${GOARCH}, such as darwin_amd64, linux_amd64
|
||||
mymath.a
|
||||
github.com/
|
||||
astaxie/
|
||||
beedb.a
|
||||
src/
|
||||
mathapp
|
||||
main.go
|
||||
mymath/
|
||||
sqrt.go
|
||||
github.com/
|
||||
astaxie/
|
||||
beedb/
|
||||
beedb.go
|
||||
util.go
|
||||
|
||||
Now you are able to see the directory structure clearly; `bin` contains executable files, `pkg` contains compiled files and `src` contains package source files.
|
||||
|
||||
(The format of environment variables in Windows is `%GOPATH%`, however this book mainly follows the Unix-style, so Windows users need to replace these yourself.)
|
||||
|
||||
## Links
|
||||
|
||||
- [Directory](preface.md)
|
||||
- Previous section: [Installation](01.1.md)
|
||||
- Next section: [Go commands](01.3.md)
|
||||
|
||||
Reference in New Issue
Block a user