Translate 01.2

This commit is contained in:
Tavee Khunbida
2019-01-12 12:47:10 +07:00
parent c9b7154b60
commit 2fd3450116
2 changed files with 43 additions and 42 deletions

View File

@@ -1,39 +1,39 @@
# 1.2 $GOPATH and workspace
# 1.2 $GOPATH และ workspace
## $GOPATH
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.
Go มีกรรมวิธีในการจัดการไฟล์ code ที่เป็นเอกลักษณ์ ด้วยการเพิ่มไดเร็คทอรีีที่เรียกว่า `$GOPATH` เพื่อใช้ในการเก็บ code ของ Go ทั้งหมดในเครื่อง ขอให้สังเกตุว่าเป็นคนละตัวกับค่าตัวแปร environment `$GOROOT` ซึ่งเป็นค่าที่บอกว่า Go ถูกติดตั้งไว้ที่ใดในเครื่อง โดยเราต้องกำหนดค่าให้กับ `$GOPATH` ก่อนที่จะสามารถใช้งานภาษาได้ โดยในระบบ *nix ทั้งหลายนั้น จะมีไฟล์ `.profile` อยู่ และเราต้องเพิ่มคำสั่ง export ที่อยู่ด้านล่างนี้เข้าไปในไฟล์ แนวคิดเบื้องหลัง gopath นี้เป็นเรื่องใหม่ ที่จะทำให้เราสามารถเชื่อมไปที่ code ของ Go ได้อย่างรวดเร็วและชัดเจน
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.
เริ่มตั้งแต่ Go 1.8 เป็นต้นมา ค่าของตัวแปร GOPATH จะถูกกำหนดค่าให้อัตโนมัติหากเราไม่ได้กำหนดให้เป็นอย่างอื่น โดยจะมีค่่าเป็น `$HOME/go` บนระบบ Unix และ `$USERPROFILE%/go` บน Windows
On Unix-like systems, the variable should be used like this:
บนระบบคล้าย Unix ทั้งหลาย ค่าตัวแปรควรจะถูกตั้งค่าดังนี้:
export GOPATH=${HOME}/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*** )
บน Windows เราจำเป็นต้องสร้างตัวแปร environment GOPATH ขึ้นมา แล้วกำหนดค่าให้เป็น `c:\mygo` ( ***ค่านี้อาจมีค่าแตกต่างออกไปขึ้นอยู่กับว่า workspace อยู่ที่ไหน*** )
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.
เป็นไปได้ที่จะมีหลาย path (workspace) ใน `$GOPATH` แต่เราต้องจำให้ได้ว่าต้องใช้ `:` (`;` สำหรับ Windows) คั่นระหว่างกลางแต่ละค่า โดยจุดนี้ `go get` จะบันทึกข้อมูลไปที่ path แรกที่ระบุใน `$GOPATH` แต่ขอแนะนำว่าไม่ควรใช้วิธีนี้ นอกจากนี้แล้วนับว่าเป็นเรื่องแย่ที่เราจะสร้างโฟลเดอร์โดยตั้งชื่อด้วยชื่อของโปรเจ็คแล้ววางไว้ใน `$GOPATH` นี่จะเป็นการทำลายทุกอย่างที่ผู้สร้างตั้งใจที่จะให้เป็นความเปลี่ยนแปลงของภาษา ด้วยเหตุผลว่าหากเราสร้างโฟลเดอร์ไว้ใน `$GOPATH` โดยตรงแล้ว เวลาที่เราจะอ้างถึง package เราจะต้องอ้างถึงโดยใช้ <packagename> โดยตรง และนี่จะทำให้เกิดปัญหาขึ้นเนื่องจาก `go get` จะไม่สามารถหา package พบ ได้โปรดทำตาม convention มันมีเหตุผลที่มันถูกสร้างขึ้นมา
In `$GOPATH`, you must have three folders as follows:
ใน `$GOPATH` นั้นต้องประกอบด้วย 3 โฟลเดอร์ดังนี้:
- `src` for source files whose suffix is .go, .c, .g, .s.
- `pkg` for compiled files whose suffix is .a.
- `bin` for executable files
- `src` สำหรับเก็บไฟล์ source code ที่มีนามสกุล .go, .c, .g, .s
- `pkg` สำหรับเก็บไฟล์ที่ได้จากการคอมไพล์ซึ่งจะมีนามสกุลเป็น .a
- `bin` สำหรับเก็บไฟล์ประเภท executable
In this book, I use `mygo` as my only path in `$GOPATH`.
ในหนังสือเล่มนี้ จะใช้ `mygo` เป็น path เดียวที่มีใน `$GOPATH`
## Package directory
## ไดเร็คทอรีที่ใช้เก็บ package
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***)
ในการสร้าง source file และโฟลเดอร์ของ package เช่น `$GOPATH/src/mymath/sqrt.go` (`mymath` คือชื่อ package) ( ***ผู้เขียนใช้ชื่อ `mymath` เป็นทั้งชื่อ package และเป็นชื่อโฟลเดอร์ที่เก็บ source file ของ package***)
ทุกครั้งที่สร้าง package ใหม่ เราควรสร้างโฟลเดอร์ใหม่ไว้ในไดเร็คทอรี `src` ยกเว้นโฟลเดอร์ main เนื่องจากโฟลเดอร์ `main` จะสร้างหรือไม่ก็ได้ไม่บังคับ โดยปรกติแล้วชื่อของโฟลเดอร์จะตั้งชื่อตามชื่อ package จะจะถูกเรียกใช้ และสามารถสร้างไดเร็คทอรีย่อยกี่ชั้นก็ได้ตามที่ต้องการ ยกตัวอย่างเช่น ถ้าสร้างไดเร็คทอรี่ `$GOPATH/src/github.com/astaxie/beedb` ชื่อ path ของ package ก็จะเป็น `github.com/astaxie/beedb` และชื่อ package เองก็คือชื่อไดเร็คทอรีตัวสุดท้ายของ path ซึ่งในกรณีนี้ก็คือ `beedb`
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.
Execute following commands. ( ***Now author goes back to talk examples*** )
รันคำสั่งดังนี้ ( ***ตอนนี้ผู้เขียนกลับมาพูดถึงตัวอย่าง*** )
cd $GOPATH/src
mkdir mymath
Create a new file called `sqrt.go`, type the following content to your file.
สร้างไฟล์ใหม่ชื่อ `sqrt.go` และเนื้อหาของไฟล์ดังนี้
```Go
// Source code of $GOPATH/src/mymath/sqrt.go
package mymath
@@ -46,33 +46,34 @@ func Sqrt(x float64) float64 {
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
ตอนนี้เราได้สร้างไดเร็คทอรีของ package และเขียน code เรียบร้อยแล้ว และขอแนะนำให้ใช้ชื่อ package เป็นชื่อเดียวกับไดเร็คทอรี และ source file ทั้งหมดของ package เก็บไว้ภายใต้ไดเร็คทอรีนี้่่
We've already created our package above, but how do we compile it for practical purposes? There are two ways to do this.
## การคอมไพล์ package
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`.
หลังจากสร้าง package ตามขั้นตอนเสร็จแล้ว เราก็ลองมาสั่งคอมไพล์ package กันดู โดยการคอมไพล์ package สามารถทำได้ 2 วิธีดังนี้:
After compiling, we can open the following folder.
1. ไปที่ path ในไดเร็คทอรีของ package แล้วใช้คำสั่ง `go install`
2. หรือใช้คำสั่งเหมือนด้านบน แต่ไม่ต้องใส่ชื่อไฟล์ เช่น `go install mymath`
หลังจากที่คอมไพล์เสร็จเรียบร้อยแล้ว ให้เข้าไปที่โฟลเดอร์ด้วยคำสั่ง
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?
จะเห็นว่ามีไฟล์ซึ่งมีนามสกุล `.a` อยู่ ซึ่งก็คือไฟล์่่ binary ของ package นั่นเอง แล้วเราจะเรียกใช้งานมันยังไงหละ?
Obviously, we need to create a new application to use it.
แน่นอนว่าเราจำเป็นต้องสร้างแอพพลิเคชั่นใหม่ขึ้นมา เพื่อเรียกใช้งาน package นี้
Create a new application package called `mathapp`.
ให้ทำการสร้างแอพพลิเคชั่นใหม่ชื่อ `mathapp`
cd $GOPATH/src
mkdir mathapp
cd mathapp
vim main.go
Write the following content to main.go.
แล้วใส่เนื้อหาดังต่อไปนี้ลงในไฟล์ main.go
```Go
@@ -89,21 +90,21 @@ func main() {
}
```
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.
ในการคอมไพล์แอพพลิเคชั่น เราต้องเข้าไปอยู่ที่ไดเร็คทอรีของแอพพลิเคชั่นที่ต้องการคอมไพล์ ซึ่งในที่นี้คือ `$GOPATH/src/mathapp` แล้วจึงสั่งคำสั่ง `go install` เมื่อคอมไพล์เสร็จแล้วจะเห็นไฟล์ที่ชื่อว่า `myapp` เกิดขึ้นมาในไดเร็คทอรี `$GOPATH/bin/` โดยเราสามารถสั่งรับแอพพลิเคชั่นโดยใช้คำสั่ง `./mathapp` เมื่อทำงานเสร็จแล้วเราควรจะเห็นข้อความปรากฎที่หน้าจอ terminal ดังนี้
Hello world. Sqrt(2) = 1.414213562373095
## Install remote packages
## การติดตั้ง package เพิ่มเติม
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 มาพร้อมกับเครื่องมือที่ใช้ในการติดตั้ง package เสริม ซึ่งได้แก่คำสั่งที่เรียกว่า `go get` โดยรองรับ opensource comunity เกือบทุกที่ รวมถึง Github, Google Code, BitBucket และ 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.
โดยสามารถใช้ `go get -u …` ในการอัพเดท package และยังสามารถติดตั้ง package ที่เป็น dependency ให้โดยอัตโนมัติด้วย
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`.
ซึ่งเครื่องมือนี้จะใช้ version control ที่แตกต่างกันสำหรับแต่ละ opensource platform ยกตัวอย่างเช่น จะใช้่่ `git` สำหรับ Github และใช้ `hg` สำหรับ Google Code ดังนั้นเราจำเป็นที่จะต้องติดตั้งเครื่องมือ version control เหล่านี้ก่อนที่เราจะใช้ `go get` ได้
After executing the above commands, the directory structure should look like following.
หลังจากที่รันคำสั่งที่กล่าวมาแล้ว โครงสร้างไดเร็คทอรีควรมีหน้าตาดังนี้
$GOPATH
src
@@ -116,15 +117,15 @@ After executing the above commands, the directory structure should look like fol
|-astaxie
|-beedb.a
Actually, `go get` clones source code to the `$GOPATH/src` of the local file system, then executes `go install`.
อันที่จริงแล้ว `go get` จะทำการ clone source code ไปเก็บไว้ที่ `$GOPATH/src` ในเครื่อง เสร็จแล้วให้สั่ง `go install` จะทำให้เราสามารถใช้ package ซึ่งมาจากที่อื่น (remote packages) ได้ในแบบเดียวกับที่เราใช้ package ที่อยู่ในเครื่องของเราเอง
You can use remote packages in the same way that we use local packages.
```Go
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
@@ -145,12 +146,12 @@ If you've followed all of the above steps, your directory structure should now l
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.
ตอนนี้เราจะเห็นโครงสร้างได้อย่างชัดเจนว่า `bin` จะเป็นที่อยู่ของไฟล์ executable ส่วน `pkg` จะเป็นที่อยู่ของไฟล์ที่ได้จากการคอมไพล์ และ `src` คือส่วนที่เก็บ source file ของ package นั่นเอง
(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.)
(รูปแบบของตัวแปร environment บน Windows คือ `%GOPATH%` แต่ว่าอย่างไรก็ตาม หนังสือเล่มนี้จะเป็นสไตล์ของ Unix ดังนี้ผู้ใช้งาน Windows จึงต้องแก้ให้ถูกต้องด้วยตัวเอง)
## Links
- [Directory](preface.md)
- Previous section: [Installation](01.1.md)
- Next section: [Go commands](01.3.md)
- บทก่อนหน้า: [การติดตั้ง](01.1.md)
- บทถัดไป: [คำสั่งของ Go](01.3.md)

View File

@@ -1,6 +1,6 @@
- 1.[การตั้งค่าสภาพแวดล้อมของ Go](01.0.md)
- 1.1. [การติดตั้ง](01.1.md)
- 1.2. [$GOPATH and workspace](01.2.md)
- 1.2. [$GOPATH และ workspace](01.2.md)
- 1.3. [Go commands](01.3.md)
- 1.4. [Go development tools](01.4.md)
- 1.5. [Summary](01.5.md)