fix grammar and typos for 01.2.md [en]
This commit is contained in:
@@ -2,15 +2,15 @@
|
||||
|
||||
## $GOPATH
|
||||
|
||||
Go commands all rely on one important environment variable which is called $GOPATH. Notice that this is not the $GOROOT where Go is installed. This variable points to the workspace of Go in your computer. (I use this path in my computer, if you don't have the same directory structure, please replace by yourself.)
|
||||
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.
|
||||
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 is OK to have more than one path(workspace) in $GOPATH, but remember that you have to use `:`(`;` in Windows) to break up them. At this point, `go get` will save the content to your first path in $GOPATH.
|
||||
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.
|
||||
|
||||
@@ -24,7 +24,7 @@ In this book, I use `mygo` as my only path in $GOPATH.
|
||||
|
||||
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, folders' name usually as same as the package's that you are going to use. You can have multi-level directory if you want to. For example, you create directories `$GOPATH/src/github.com/astaxie/beedb`, then the package path is `github.com/astaxie/beedb`. The package name will be the last directory in your path, which is `beedb` in this case.
|
||||
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*** )
|
||||
|
||||
@@ -44,22 +44,22 @@ Create a new file called `sqrt.go`, type following content to your file.
|
||||
return z
|
||||
}
|
||||
|
||||
Now my package directory is created and code work is done. I recommend you to keep same name for your package and the folder contains package source files.
|
||||
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 to compile it for practical? There are two ways to do it.
|
||||
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 command `go install`.
|
||||
2. Execute above command with file name like `go install mymath`.
|
||||
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 compiled, we can open the following folder.
|
||||
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 packages, and now how can we use it?
|
||||
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.
|
||||
|
||||
@@ -84,21 +84,21 @@ code
|
||||
fmt.Printf("Hello, world. Sqrt(2) = %v\n", mymath.Sqrt(2))
|
||||
}
|
||||
|
||||
To compile this application, you need to switch to the application directory which is `$GOPATH/src/mathapp` in this case, then execute command `go install`. Now you should see an executable file called `mathapp` was generated in the directory `$GOPATH/bin/`. To run this program, use command `./mathapp`, you should see following content in your terminal.
|
||||
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 the command called `go get`. It supports most of open source communities, including Github, Google Code, BitBucket, and Launchpad.
|
||||
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.
|
||||
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, `hg` for Google Code. Therefore you have to install these version control tools before you use `go get`.
|
||||
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 executed above commands, the directory structure should look like following.
|
||||
After executing the above commands, the directory structure should look like following.
|
||||
|
||||
$GOPATH
|
||||
src
|
||||
@@ -111,15 +111,15 @@ After executed above commands, the directory structure should look like followin
|
||||
|-astaxie
|
||||
|-beedb.a
|
||||
|
||||
Actually, `go get` clones source code to $GOPATH/src of local file system, then executes `go install`.
|
||||
Actually, `go get` clones source code to the $GOPATH/src of the local file system, then executes `go install`.
|
||||
|
||||
Use remote packages is the same way as we use local packages.
|
||||
You can use remote packages in the same way that we use local packages.
|
||||
|
||||
import "github.com/astaxie/beedb"
|
||||
|
||||
## Directory complete structure
|
||||
|
||||
If you follow all steps, your directory structure should look like the following now.
|
||||
If you've followed all of the above steps, your directory structure should now look like the following.
|
||||
|
||||
bin/
|
||||
mathapp
|
||||
@@ -140,9 +140,9 @@ If you follow all steps, your directory structure should look like the following
|
||||
beedb.go
|
||||
util.go
|
||||
|
||||
Now you are able to see the directory structure clearly, `bin` contains executable files, `pkg` contains compiled files, `src` contains package source files.
|
||||
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%`, this book mainly uses Unix-style, so Windows users need to replace by yourself.)
|
||||
(The format of environment variables in Windows is `%GOPATH%`, however this book mainly folows the Unix-style, so Windows users need to replace by yourself.)
|
||||
|
||||
## Links
|
||||
|
||||
|
||||
Reference in New Issue
Block a user