Skip to content

Commit 454ac1f

Browse files
committed
initial thai translation
1 parent 55e90cb commit 454ac1f

File tree

469 files changed

+17014
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

469 files changed

+17014
-0
lines changed

th/01.0.md

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 1 Go Environment Configuration
2+
3+
Welcome to the world of Go, let's start exploring!
4+
5+
Go is a fast-compiled, garbage-collected, concurrent systems programming language. It has the following advantages:
6+
7+
- Compiles a large project within a few seconds.
8+
- Provides a software development model that is easy to reason about, avoiding most of the problems associated with C-style header files.
9+
- Is a static language that does not have levels in its type system, so users do not need to spend much time dealing with relations between types. It is more like a lightweight object-oriented language.
10+
- Performs garbage collection. It provides basic support for concurrency and communication.
11+
- Designed for multi-core computers.
12+
13+
Go is a compiled language. It combines the development efficiency of interpreted or dynamic languages with the security of static languages. It is going to be the language of choice for modern, multi-core computers with networking. For these purposes, there are some problems that need to inherently be resolved at the level of the language of choice, such as a richly expressive lightweight type system, a native concurrency model, and strictly regulated garbage collection. For quite some time, no packages or tools have emerged that have aimed to solve all of these problems in a pragmatic fashion; thus was born the motivation for the Go language.
14+
15+
In this chapter, I will show you how to install and configure your own Go development environment.
16+
17+
## Links
18+
19+
- [Directory](preface.md)
20+
- Next section: [Installation](01.1.md)

th/01.1.md

+148
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
# 1.1 Installation
2+
3+
## Three ways to install Go
4+
5+
There are many ways to configure the Go development environment on your computer, and you can choose whichever one you like. The three most common ways are as follows.
6+
7+
8+
- Official installation packages.
9+
- The Go team provides convenient installation packages in Windows, Linux, Mac and other operating systems. This is probably the easiest way to get started. You can get the installers from the [Golang Download Page](https://golang.org/dl/).
10+
- Install it yourself from source code.
11+
- Popular with developers who are familiar with Unix-like systems.
12+
- Using third-party tools.
13+
- There are many third-party tools and package managers for installing Go, like apt-get in Ubuntu and homebrew for Mac.
14+
15+
In case you want to install more than one version of Go on a computer, you should take a look at a tool called [GVM](https://github.com/moovweb/gvm). It is the best tool I've seen so far for accomplishing this task, otherwise you'd have to deal with it yourself.
16+
17+
## Install from source code
18+
19+
To compile Go 1.5 and upwards, you only need the previous version of Go, as Go has achieved bootstrapping. You only need Go to compile Go.
20+
21+
To compile Go 1.4 downwards, you will need a C compiler as some parts of Go are still written in Plan 9 C and AT&T assembler.
22+
23+
On a Mac, if you have installed Xcode, you already have the compiler.
24+
25+
On Unix-like systems, you need to install gcc or a similar compiler. For example, using the package manager apt-get (included with Ubuntu), one can install the required compilers as follows:
26+
27+
```sh
28+
sudo apt-get install gcc libc6-dev
29+
```
30+
31+
On Windows, you need to install MinGW in order to install gcc. Don't forget to configure your environment variables after the installation has completed.( ***Everything that looks like this means it's commented by a translator: If you are using 64-bit Windows, you should install the 64-bit version of MinGW*** )
32+
33+
At this point, execute the following commands to clone the Go source code and compile it.( ***It will clone the source code to your current directory. Switch your work path before you continue. This may take some time.*** )
34+
35+
git clone https://go.googlesource.com/go
36+
cd go/src
37+
./all.bash
38+
39+
A successful installation will end with the message "ALL TESTS PASSED."
40+
41+
On Windows, you can achieve the same by running `all.bat`.
42+
43+
If you are using Windows, the installation package will set your environment variables automatically. In Unix-like systems, you need to set these variables manually as follows. ( ***If your Go version is greater than 1.0, you don't have to set $GOBIN, and it will automatically be related to your $GOROOT/bin, which we will talk about in the next section***)
44+
45+
export GOROOT=$HOME/go
46+
export GOBIN=$GOROOT/bin
47+
export PATH=$PATH:$GOROOT/bin
48+
49+
If you see the following information on your screen, you're all set.
50+
51+
![](images/1.1.mac.png?raw=true)
52+
53+
Figure 1.1 Information after installing from source code
54+
55+
Once you see the usage information of Go, it means you have successfully installed Go on your computer. If it says "no such command", check that your $PATH environment variable contains the installation path of Go.
56+
57+
## Using the standard installation packages
58+
59+
Go has one-click installation packages for every supported operating system. These packages will install Go in `/usr/local/go` (`c:\Go` in Windows) by default. Of course this can be modified, but you also need to change all the environment variables manually as I've shown above.
60+
61+
### How to check if your operating system is 32-bit or 64-bit?
62+
63+
Our next step depends on your operating system type, so we have to check it before we download the standard installation packages.
64+
65+
If you are using Windows, press `Win+R` and then run the command tool. Type the `systeminfo` command and it will show you some useful system information. Find the line that says "system type" -if you see "x64-based PC" that means your operating system is 64-bit, 32-bit otherwise.
66+
67+
I strongly recommend downloading the 64-bit package if you are a Mac user, as Go no longer supports pure 32-bit processors on Mac OSX.
68+
69+
Linux users can type `uname -a` in the terminal to see system information.
70+
A 64-bit operating system will show the following:
71+
72+
<some description> x86_64 x86_64 x86_64 GNU/Linux
73+
// some machines such as Ubuntu 10.04 will show as following
74+
x86_64 GNU/Linux
75+
76+
32-bit operating systems instead show:
77+
78+
<some description> i686 i686 i386 GNU/Linux
79+
80+
### Mac
81+
82+
Go to the [download page](https://golang.org/dl/), choose `go1.4.2.darwin-386.pkg` (The later version has no 32-bit download.)for 32-bit systems and `go1.8.3.darwin-amd64.pkg` for 64-bit systems. Going all the way to the end by clicking "next", `~/go/bin` will be added to your system's $PATH after you finish the installation. Now open the terminal and type `go`. You should see the same output shown in figure 1.1.
83+
84+
### Linux
85+
86+
Go to the [download page](https://golang.org/dl/), choose `go1.8.3.linux-386.tar.gz` for 32-bit systems and `go1.8.3.linux-amd64.tar.gz` for 64-bit systems. Suppose you want to install Go in the `$GO_INSTALL_DIR` path. Uncompress the `tar.gz` to your chosen path using the command `tar zxvf go1.8.3.linux-amd64.tar.gz -C $GO_INSTALL_DIR`. Then set your $PATH with the following: `export PATH=$PATH:$GO_INSTALL_DIR/go/bin`. Now just open the terminal and type `go`. You should now see the same output displayed in figure 1.1.
87+
88+
### Windows
89+
90+
Go to the [download page](https://golang.org/dl/), choose `go1.8.3.windows-386.msi` for 32-bit systems and `go1.8.3.windows-amd64.msi` for 64-bit systems. Going all the way to the end by clicking "next", `c:/go/bin` will be added to `path`. Now just open a command line window and type `go`. You should now see the same output displayed in figure 1.1.
91+
92+
## Use third-party tools
93+
94+
### GVM
95+
96+
GVM is a Go multi-version control tool developed by a third-party, like rvm for ruby. It's quite easy to use. Install gvm by typing the following commands in your terminal:
97+
98+
bash < <(curl -s -S -L https://raw.github.com/moovweb/gvm/master/binscripts/gvm-installer)
99+
100+
Then we install Go using the following commands:
101+
102+
gvm install go1.8.3
103+
gvm use go1.8.3
104+
105+
After the process has finished, you're all set.
106+
107+
### apt-get
108+
109+
Ubuntu is the most popular desktop release version of Linux. It uses `apt-get` to manage packages. We can install Go using the following commands.
110+
111+
sudo add-apt-repository ppa:gophers/go
112+
sudo apt-get update
113+
sudo apt-get install golang-go
114+
115+
### wget
116+
```sh
117+
118+
wget https://storage.googleapis.com/golang/go1.8.3.linux-amd64.tar.gz
119+
sudo tar -xzf go1.8.3.linux-amd64.tar.gz -C /usr/local
120+
121+
# Go environment
122+
export GOROOT=/usr/local/go
123+
export GOBIN=$GOROOT/bin
124+
export PATH=$PATH:$GOBIN
125+
export GOPATH=$HOME/gopath
126+
```
127+
Starting from go 1.8, The GOPATH environment variable now has a default value if it is unset. It defaults to `$HOME/go` on Unix and `%USERPROFILE%/go` on Windows.
128+
### Homebrew
129+
130+
Homebrew is a software management tool commonly used in Mac to manage packages. Just type the following commands to install Go.
131+
132+
1. Install Homebrew
133+
134+
```sh
135+
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
136+
```
137+
138+
2. Install Go
139+
140+
```sh
141+
brew update && brew upgrade
142+
brew install go
143+
```
144+
## Links
145+
146+
- [Directory](preface.md)
147+
- Previous section: [Go environment configuration](01.0.md)
148+
- Next section: [$GOPATH and workspace](01.2.md)

th/01.2.md

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
# 1.2 $GOPATH and workspace
2+
3+
## $GOPATH
4+
5+
Go takes a unique approach to manage the code files with the introduction of a `$GOPATH` directory which contains all the go code on the machine. Note that this is different from the `$GOROOT` environment variable which states where go is installed on the machine. We have to define the `$GOPATH` variable before using the language, in *nix systems there is a file called `.profile` we need to append the below export statement to the file. The concept behind gopath is a novel one, where we can link to any go code at any instant of time without ambiguity.
6+
7+
Starting from go 1.8, the GOPATH environment variable now has a default value if it not set: it defaults to `$HOME/go` on Unix and `%USERPROFILE%/go` on Windows.
8+
9+
On Unix-like systems, the variable should be used like this:
10+
11+
export GOPATH=${HOME}/mygo
12+
13+
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*** )
14+
15+
It's OK to have more than one path (workspace) in `$GOPATH`, but remember that you have to use `:`(`;` in Windows) to separate them. At this point, `go get` will save the content to your first path in `$GOPATH`. It is highly recommended to not have multiples versions, the worst case is to create a folder by the name of your project right inside `$GOPATH`, it breaks everything that the creators were wishing to change in programming with the creation of go language because when you create a folder inside `$GOPATH` you will reference your packages as directly as <packagename>, and this breaks all the applications which will import your package because the `go get` won't find your package. Please follow conventions, there is a reason conventions are created.
16+
17+
In `$GOPATH`, you must have three folders as follows:
18+
19+
- `src` for source files whose suffix is .go, .c, .g, .s.
20+
- `pkg` for compiled files whose suffix is .a.
21+
- `bin` for executable files
22+
23+
In this book, I use `mygo` as my only path in `$GOPATH`.
24+
25+
## Package directory
26+
27+
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 the same name for the folder that contains the package source files***)
28+
29+
Every time you create a package, you should create a new folder in the `src` directory, with the notable exception of main, for which `main` folder creation is optional. 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.
30+
31+
Execute following commands. ( ***Now author goes back to talk examples*** )
32+
33+
cd $GOPATH/src
34+
mkdir mymath
35+
36+
Create a new file called `sqrt.go`, type the following content to your file.
37+
```Go
38+
// Source code of $GOPATH/src/mymath/sqrt.go
39+
package mymath
40+
41+
func Sqrt(x float64) float64 {
42+
z := 0.0
43+
for i := 0; i < 1000; i++ {
44+
z -= (z*z - x) / (2 * x)
45+
}
46+
return z
47+
}
48+
```
49+
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.
50+
51+
## Compile packages
52+
53+
We've already created our package above, but how do we compile it for practical purposes? There are two ways to do this.
54+
55+
1. Switch your work path to the directory of your package, then execute the `go install` command.
56+
2. Execute the above command except with a file name, like `go install mymath`.
57+
58+
After compiling, we can open the following folder.
59+
60+
cd $GOPATH/pkg/${GOOS}_${GOARCH}
61+
// you can see the file was generated
62+
mymath.a
63+
64+
The file whose suffix is `.a` is the binary file of our package. How do we use it?
65+
66+
Obviously, we need to create a new application to use it.
67+
68+
Create a new application package called `mathapp`.
69+
70+
cd $GOPATH/src
71+
mkdir mathapp
72+
cd mathapp
73+
vim main.go
74+
75+
Write the following content to main.go.
76+
77+
```Go
78+
79+
//$GOPATH/src/mathapp/main.go source code.
80+
package main
81+
82+
import (
83+
"mymath"
84+
"fmt"
85+
)
86+
87+
func main() {
88+
fmt.Printf("Hello, world. Sqrt(2) = %v\n", mymath.Sqrt(2))
89+
}
90+
```
91+
92+
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.
93+
94+
Hello world. Sqrt(2) = 1.414213562373095
95+
96+
## Install remote packages
97+
98+
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.
99+
100+
go get github.com/astaxie/beedb
101+
102+
You can use `go get -u …` to update your remote packages and it will automatically install all the dependent packages as well.
103+
104+
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`.
105+
106+
After executing the above commands, the directory structure should look like following.
107+
108+
$GOPATH
109+
src
110+
|-github.com
111+
|-astaxie
112+
|-beedb
113+
pkg
114+
|--${GOOS}_${GOARCH}
115+
|-github.com
116+
|-astaxie
117+
|-beedb.a
118+
119+
Actually, `go get` clones source code to the `$GOPATH/src` of the local file system, then executes `go install`.
120+
121+
You can use remote packages in the same way that we use local packages.
122+
```Go
123+
import "github.com/astaxie/beedb"
124+
```
125+
## Directory complete structure
126+
127+
If you've followed all of the above steps, your directory structure should now look like the following.
128+
129+
bin/
130+
mathapp
131+
pkg/
132+
${GOOS}_${GOARCH}, such as darwin_amd64, linux_amd64
133+
mymath.a
134+
github.com/
135+
astaxie/
136+
beedb.a
137+
src/
138+
mathapp
139+
main.go
140+
mymath/
141+
sqrt.go
142+
github.com/
143+
astaxie/
144+
beedb/
145+
beedb.go
146+
util.go
147+
148+
Now you are able to see the directory structure clearly: `bin` contains executable files, `pkg` contains compiled files and `src` contains package source files.
149+
150+
(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.)
151+
152+
## Links
153+
154+
- [Directory](preface.md)
155+
- Previous section: [Installation](01.1.md)
156+
- Next section: [Go commands](01.3.md)

0 commit comments

Comments
 (0)