You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 ได้อย่างรวดเร็วและชัดเจน
6
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.
7
+
เริ่มตั้งแต่ Go 1.8 เป็นต้นมา ค่าของตัวแปร GOPATH จะถูกกำหนดค่าให้อัตโนมัติหากเราไม่ได้กำหนดให้เป็นอย่างอื่น โดยจะมีค่่าเป็น `$HOME/go`บนระบบ Unix และ `$USERPROFILE%/go`บน Windows
8
8
9
-
On Unix-like systems, the variable should be used like this:
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 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.
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***)
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*** )
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.
Create a new application package called `mathapp`.
69
+
ให้ทำการสร้างแอพพลิเคชั่นใหม่ชื่อ `mathapp`
69
70
70
71
cd $GOPATH/src
71
72
mkdir mathapp
72
73
cd mathapp
73
74
vim main.go
74
75
75
-
Write the following content to main.go.
76
+
แล้วใส่เนื้อหาดังต่อไปนี้ลงในไฟล์ main.go
76
77
77
78
```Go
78
79
@@ -89,21 +90,21 @@ func main() {
89
90
}
90
91
```
91
92
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.
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
99
100
100
101
go get github.com/astaxie/beedb
101
102
102
-
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`.
105
+
ซึ่งเครื่องมือนี้จะใช้ version control ที่แตกต่างกันสำหรับแต่ละ opensource platform ยกตัวอย่างเช่น จะใช้่่ `git`สำหรับ Github และใช้`hg`สำหรับ Google Code ดังนั้นเราจำเป็นที่จะต้องติดตั้งเครื่องมือ version control เหล่านี้ก่อนที่เราจะใช้ `go get` ได้
105
106
106
-
After executing the above commands, the directory structure should look like following.
@@ -145,12 +146,12 @@ If you've followed all of the above steps, your directory structure should now l
145
146
beedb.go
146
147
util.go
147
148
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.
(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 จึงต้องแก้ให้ถูกต้องด้วยตัวเอง)
0 commit comments