Skip to content

Commit 2fd3450

Browse files
committed
Translate 01.2
1 parent c9b7154 commit 2fd3450

File tree

2 files changed

+43
-42
lines changed

2 files changed

+43
-42
lines changed

th/01.2.md

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
# 1.2 $GOPATH and workspace
1+
# 1.2 $GOPATH และ workspace
22

33
## $GOPATH
44

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.
5+
Go มีกรรมวิธีในการจัดการไฟล์ code ที่เป็นเอกลักษณ์ ด้วยการเพิ่มไดเร็คทอรีีที่เรียกว่า `$GOPATH` เพื่อใช้ในการเก็บ code ของ Go ทั้งหมดในเครื่อง ขอให้สังเกตุว่าเป็นคนละตัวกับค่าตัวแปร environment `$GOROOT` ซึ่งเป็นค่าที่บอกว่า Go ถูกติดตั้งไว้ที่ใดในเครื่อง โดยเราต้องกำหนดค่าให้กับ `$GOPATH` ก่อนที่จะสามารถใช้งานภาษาได้ โดยในระบบ *nix ทั้งหลายนั้น จะมีไฟล์ `.profile` อยู่ และเราต้องเพิ่มคำสั่ง export ที่อยู่ด้านล่างนี้เข้าไปในไฟล์ แนวคิดเบื้องหลัง gopath นี้เป็นเรื่องใหม่ ที่จะทำให้เราสามารถเชื่อมไปที่ code ของ Go ได้อย่างรวดเร็วและชัดเจน
66

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.
7+
เริ่มตั้งแต่ Go 1.8 เป็นต้นมา ค่าของตัวแปร GOPATH จะถูกกำหนดค่าให้อัตโนมัติหากเราไม่ได้กำหนดให้เป็นอย่างอื่น โดยจะมีค่่าเป็น `$HOME/go` บนระบบ Unix และ `$USERPROFILE%/go` บน Windows
88

9-
On Unix-like systems, the variable should be used like this:
9+
บนระบบคล้าย Unix ทั้งหลาย ค่าตัวแปรควรจะถูกตั้งค่าดังนี้:
1010

1111
export GOPATH=${HOME}/mygo
1212

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*** )
13+
บน Windows เราจำเป็นต้องสร้างตัวแปร environment GOPATH ขึ้นมา แล้วกำหนดค่าให้เป็น `c:\mygo` ( ***ค่านี้อาจมีค่าแตกต่างออกไปขึ้นอยู่กับว่า workspace อยู่ที่ไหน*** )
1414

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.
15+
เป็นไปได้ที่จะมีหลาย path (workspace) ใน `$GOPATH` แต่เราต้องจำให้ได้ว่าต้องใช้ `:` (`;` สำหรับ Windows) คั่นระหว่างกลางแต่ละค่า โดยจุดนี้ `go get` จะบันทึกข้อมูลไปที่ path แรกที่ระบุใน `$GOPATH` แต่ขอแนะนำว่าไม่ควรใช้วิธีนี้ นอกจากนี้แล้วนับว่าเป็นเรื่องแย่ที่เราจะสร้างโฟลเดอร์โดยตั้งชื่อด้วยชื่อของโปรเจ็คแล้ววางไว้ใน `$GOPATH` นี่จะเป็นการทำลายทุกอย่างที่ผู้สร้างตั้งใจที่จะให้เป็นความเปลี่ยนแปลงของภาษา ด้วยเหตุผลว่าหากเราสร้างโฟลเดอร์ไว้ใน `$GOPATH` โดยตรงแล้ว เวลาที่เราจะอ้างถึง package เราจะต้องอ้างถึงโดยใช้ <packagename> โดยตรง และนี่จะทำให้เกิดปัญหาขึ้นเนื่องจาก `go get` จะไม่สามารถหา package พบ ได้โปรดทำตาม convention มันมีเหตุผลที่มันถูกสร้างขึ้นมา
1616

17-
In `$GOPATH`, you must have three folders as follows:
17+
ใน `$GOPATH` นั้นต้องประกอบด้วย 3 โฟลเดอร์ดังนี้:
1818

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
19+
- `src` สำหรับเก็บไฟล์ source code ที่มีนามสกุล .go, .c, .g, .s
20+
- `pkg` สำหรับเก็บไฟล์ที่ได้จากการคอมไพล์ซึ่งจะมีนามสกุลเป็น .a
21+
- `bin` สำหรับเก็บไฟล์ประเภท executable
2222

23-
In this book, I use `mygo` as my only path in `$GOPATH`.
23+
ในหนังสือเล่มนี้ จะใช้ `mygo` เป็น path เดียวที่มีใน `$GOPATH`
2424

25-
## Package directory
25+
## ไดเร็คทอรีที่ใช้เก็บ package
2626

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***)
27+
ในการสร้าง source file และโฟลเดอร์ของ package เช่น `$GOPATH/src/mymath/sqrt.go` (`mymath` คือชื่อ package) ( ***ผู้เขียนใช้ชื่อ `mymath` เป็นทั้งชื่อ package และเป็นชื่อโฟลเดอร์ที่เก็บ source file ของ package***)
28+
ทุกครั้งที่สร้าง package ใหม่ เราควรสร้างโฟลเดอร์ใหม่ไว้ในไดเร็คทอรี `src` ยกเว้นโฟลเดอร์ main เนื่องจากโฟลเดอร์ `main` จะสร้างหรือไม่ก็ได้ไม่บังคับ โดยปรกติแล้วชื่อของโฟลเดอร์จะตั้งชื่อตามชื่อ package จะจะถูกเรียกใช้ และสามารถสร้างไดเร็คทอรีย่อยกี่ชั้นก็ได้ตามที่ต้องการ ยกตัวอย่างเช่น ถ้าสร้างไดเร็คทอรี่ `$GOPATH/src/github.com/astaxie/beedb` ชื่อ path ของ package ก็จะเป็น `github.com/astaxie/beedb` และชื่อ package เองก็คือชื่อไดเร็คทอรีตัวสุดท้ายของ path ซึ่งในกรณีนี้ก็คือ `beedb`
2829

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*** )
30+
รันคำสั่งดังนี้ ( ***ตอนนี้ผู้เขียนกลับมาพูดถึงตัวอย่าง*** )
3231

3332
cd $GOPATH/src
3433
mkdir mymath
3534

36-
Create a new file called `sqrt.go`, type the following content to your file.
35+
สร้างไฟล์ใหม่ชื่อ `sqrt.go` และเนื้อหาของไฟล์ดังนี้
36+
3737
```Go
3838
// Source code of $GOPATH/src/mymath/sqrt.go
3939
package mymath
@@ -46,33 +46,34 @@ func Sqrt(x float64) float64 {
4646
return z
4747
}
4848
```
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.
5049

51-
## Compile packages
50+
ตอนนี้เราได้สร้างไดเร็คทอรีของ package และเขียน code เรียบร้อยแล้ว และขอแนะนำให้ใช้ชื่อ package เป็นชื่อเดียวกับไดเร็คทอรี และ source file ทั้งหมดของ package เก็บไว้ภายใต้ไดเร็คทอรีนี้่่
5251

53-
We've already created our package above, but how do we compile it for practical purposes? There are two ways to do this.
52+
## การคอมไพล์ package
5453

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`.
54+
หลังจากสร้าง package ตามขั้นตอนเสร็จแล้ว เราก็ลองมาสั่งคอมไพล์ package กันดู โดยการคอมไพล์ package สามารถทำได้ 2 วิธีดังนี้:
5755

58-
After compiling, we can open the following folder.
56+
1. ไปที่ path ในไดเร็คทอรีของ package แล้วใช้คำสั่ง `go install`
57+
2. หรือใช้คำสั่งเหมือนด้านบน แต่ไม่ต้องใส่ชื่อไฟล์ เช่น `go install mymath`
58+
59+
หลังจากที่คอมไพล์เสร็จเรียบร้อยแล้ว ให้เข้าไปที่โฟลเดอร์ด้วยคำสั่ง
5960

6061
cd $GOPATH/pkg/${GOOS}_${GOARCH}
6162
// you can see the file was generated
6263
mymath.a
6364

64-
The file whose suffix is `.a` is the binary file of our package. How do we use it?
65+
จะเห็นว่ามีไฟล์ซึ่งมีนามสกุล `.a` อยู่ ซึ่งก็คือไฟล์่่ binary ของ package นั่นเอง แล้วเราจะเรียกใช้งานมันยังไงหละ?
6566

66-
Obviously, we need to create a new application to use it.
67+
แน่นอนว่าเราจำเป็นต้องสร้างแอพพลิเคชั่นใหม่ขึ้นมา เพื่อเรียกใช้งาน package นี้
6768

68-
Create a new application package called `mathapp`.
69+
ให้ทำการสร้างแอพพลิเคชั่นใหม่ชื่อ `mathapp`
6970

7071
cd $GOPATH/src
7172
mkdir mathapp
7273
cd mathapp
7374
vim main.go
7475

75-
Write the following content to main.go.
76+
แล้วใส่เนื้อหาดังต่อไปนี้ลงในไฟล์ main.go
7677

7778
```Go
7879

@@ -89,21 +90,21 @@ func main() {
8990
}
9091
```
9192

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+
ในการคอมไพล์แอพพลิเคชั่น เราต้องเข้าไปอยู่ที่ไดเร็คทอรีของแอพพลิเคชั่นที่ต้องการคอมไพล์ ซึ่งในที่นี้คือ `$GOPATH/src/mathapp` แล้วจึงสั่งคำสั่ง `go install` เมื่อคอมไพล์เสร็จแล้วจะเห็นไฟล์ที่ชื่อว่า `myapp` เกิดขึ้นมาในไดเร็คทอรี `$GOPATH/bin/` โดยเราสามารถสั่งรับแอพพลิเคชั่นโดยใช้คำสั่ง `./mathapp` เมื่อทำงานเสร็จแล้วเราควรจะเห็นข้อความปรากฎที่หน้าจอ terminal ดังนี้
9394

9495
Hello world. Sqrt(2) = 1.414213562373095
9596

96-
## Install remote packages
97+
## การติดตั้ง package เพิ่มเติม
9798

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+
Go มาพร้อมกับเครื่องมือที่ใช้ในการติดตั้ง package เสริม ซึ่งได้แก่คำสั่งที่เรียกว่า `go get` โดยรองรับ opensource comunity เกือบทุกที่ รวมถึง Github, Google Code, BitBucket และ Launchpad
99100

100101
go get github.com/astaxie/beedb
101102

102-
You can use `go get -u …` to update your remote packages and it will automatically install all the dependent packages as well.
103+
โดยสามารถใช้ `go get -u …` ในการอัพเดท package และยังสามารถติดตั้ง package ที่เป็น dependency ให้โดยอัตโนมัติด้วย
103104

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+
ซึ่งเครื่องมือนี้จะใช้ version control ที่แตกต่างกันสำหรับแต่ละ opensource platform ยกตัวอย่างเช่น จะใช้่่ `git` สำหรับ Github และใช้ `hg` สำหรับ Google Code ดังนั้นเราจำเป็นที่จะต้องติดตั้งเครื่องมือ version control เหล่านี้ก่อนที่เราจะใช้ `go get` ได้
105106

106-
After executing the above commands, the directory structure should look like following.
107+
หลังจากที่รันคำสั่งที่กล่าวมาแล้ว โครงสร้างไดเร็คทอรีควรมีหน้าตาดังนี้
107108

108109
$GOPATH
109110
src
@@ -116,15 +117,15 @@ After executing the above commands, the directory structure should look like fol
116117
|-astaxie
117118
|-beedb.a
118119
119-
Actually, `go get` clones source code to the `$GOPATH/src` of the local file system, then executes `go install`.
120+
อันที่จริงแล้ว `go get` จะทำการ clone source code ไปเก็บไว้ที่ `$GOPATH/src` ในเครื่อง เสร็จแล้วให้สั่ง `go install` จะทำให้เราสามารถใช้ package ซึ่งมาจากที่อื่น (remote packages) ได้ในแบบเดียวกับที่เราใช้ package ที่อยู่ในเครื่องของเราเอง
120121

121-
You can use remote packages in the same way that we use local packages.
122122
```Go
123123
import "github.com/astaxie/beedb"
124124
```
125-
## Directory complete structure
126125

127-
If you've followed all of the above steps, your directory structure should now look like the following.
126+
## โครงสร้างไดเร็คทอรี่แบบสมบูรณ์
127+
128+
หากเราทำตามคำสั่งที่กล่าวมาแล้วทั้งหมด โครงสร้างไดเร็คทอรีในเครื่องของเราควรจะมีลักษณะดังนี้
128129

129130
bin/
130131
mathapp
@@ -145,12 +146,12 @@ If you've followed all of the above steps, your directory structure should now l
145146
beedb.go
146147
util.go
147148
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+
ตอนนี้เราจะเห็นโครงสร้างได้อย่างชัดเจนว่า `bin` จะเป็นที่อยู่ของไฟล์ executable ส่วน `pkg` จะเป็นที่อยู่ของไฟล์ที่ได้จากการคอมไพล์ และ `src` คือส่วนที่เก็บ source file ของ package นั่นเอง
149150

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+
(รูปแบบของตัวแปร environment บน Windows คือ `%GOPATH%` แต่ว่าอย่างไรก็ตาม หนังสือเล่มนี้จะเป็นสไตล์ของ Unix ดังนี้ผู้ใช้งาน Windows จึงต้องแก้ให้ถูกต้องด้วยตัวเอง)
151152

152153
## Links
153154

154155
- [Directory](preface.md)
155-
- Previous section: [Installation](01.1.md)
156-
- Next section: [Go commands](01.3.md)
156+
- บทก่อนหน้า: [การติดตั้ง](01.1.md)
157+
- บทถัดไป: [คำสั่งของ Go](01.3.md)

th/preface.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
- 1.[การตั้งค่าสภาพแวดล้อมของ Go](01.0.md)
22
- 1.1. [การติดตั้ง](01.1.md)
3-
- 1.2. [$GOPATH and workspace](01.2.md)
3+
- 1.2. [$GOPATH และ workspace](01.2.md)
44
- 1.3. [Go commands](01.3.md)
55
- 1.4. [Go development tools](01.4.md)
66
- 1.5. [Summary](01.5.md)

0 commit comments

Comments
 (0)