Update 01.3

This commit is contained in:
Tavee Khunbida
2019-01-18 09:27:37 +07:00
parent 52a3a48cf7
commit 736f5bf156

View File

@@ -12,92 +12,93 @@
## go build
This command is for compiling tests. It will compile packages and dependencies if it's necessary.
คำสั่งนี้จะเป็นการคอมไพล์ ซึ่งจะคอมไพล์ทั้ง package เองและ package ที่เป็น dependency ด้วย ถ้าหากจำเป็น
- If the package is not the `main` package such as `mymath` in section 1.2, nothing will be generated after you execute `go build`. If you need the package file `.a` in `$GOPATH/pkg`, use `go install` instead.
- If the package is the `main` package, it will generate an executable file in the same folder. If you want the file to be generated in `$GOPATH/bin`, use `go install` or `go build -o ${PATH_HERE}/a.exe.`
- If there are many files in the folder, but you just want to compile one of them, you should append the file name after `go build`. For example, `go build a.go`. `go build` will compile all the files in the folder.
- You can also assign the name of the file that will be generated. For instance, in the `mathapp` project (in section 1.2), using `go build -o astaxie.exe` will generate `astaxie.exe` instead of `mathapp.exe`. The default name is your folder name (non-main package) or the first source file name (main package).
- ถ้า package ไม่ใช่ package `main` ยกตัวอย่างเช่น `mymath` ใน section 1.2 จะไม่มีอะไรถูกสร้างขึ้นมาหลังการรันคำสั่ง `go build` หากเราต้องการได้ไฟล์ package `.a` ใน `$GOPATH/pkg` ให้ใช้คำสั่ง `go install` แทน
- ในการณีที่เป็น `main` package แล้ว จะมีการสร้างไฟล์ executable ไว้ภายใต้โฟลเดอร์เดียวกัน แต่หากเราต้องการให้ถูกสร้างไว้ที่ `$GOPATH/bin` ให้ใช้คำสั่ง `go install` หรือ `go build -o ${PATH_HERE}/a.exe` แทน
- ในการณีที่มีไฟล์อยู่ในโฟลเดอร์หลายไฟล์ แต่เราต้องการที่จะคอมไพล์แค่ไฟล์เดียว สามารถทำได้โดยการเพิ่มชื่อไฟล์ต่อเข้าไปหลัง `go build` ยกตัวอย่างเช่น `go build a.go` ซึ่งหากรันแค่คำสั่ง `go build` จะเป็นการคอมไพล์ทุกไฟล์ในโฟลเดอร์นั้น
- นอกจากนี้เรายังสามารถกำหนดชื่อให้กับไฟล์ที่ถูกสร้างขึ้นมาได้ด้วย อย่างเช่น ในโปรเจ็ค `mathapp` (จาก section 1.2) การใช้คำสั่ง `go build -o astaxie.exe` จะมีไฟล์ `astaxie.exe` ถูกสร้างขึ้นมาแทนที่จะเป็น `mathapp.exe` ซึ่งจะสังเกตได้ว่าชื่อโฟลเดอร์จะถูกใช้โดยปริยายหากไม่ได้กำหนดเป็นค่าอื่น (กรณีไม่ใช่ main package) หรือจะใช้ชื่อตาม source file ไฟล์แรก (กรณี main package)
(According to [The Go Programming Language Specification](https://golang.org/ref/spec), package names should be the name after the word `package` in the first line of your source files. It doesn't have to be the same as the folder name, and the executable file name will be your folder name by default.)
(อ้างอิงจาก [The Go Programming Language Specification](https://golang.org/ref/spec) ชื่อ package ควรจะเป็นคำที่อยู่ต่อจาก `package` ในบรรทัดแรกของ source file ซึ่งไม่จำเป็นว่าต้องเป็นชื่อเดียวกับชื่อโฟลเดอร์ แต่ไฟล์ executable ที่ได้จากการคอมไพล์จะใช้ชื่อโฟลเดอร์เป็นค่าโดยปริยาย)
- `go build` ignores files whose names start with `_` or `.`.
- If you want to have different source files for every operating system, you can name files with the system name as a suffix. Suppose there are some source files for loading arrays. They could be named as follows:
- `go build` จะไม่สนใจไฟล์ที่ชื่อขึ้นต้นด้วย `_` หรือ `.`
- หากเราต้องการจะใช้ไฟล์ชื่อแตกต่างกันตามระบบปฎิบัติการแล้วหละก็ สามารถตั้งชื่อไฟล์โดยใส่ suffix ได้ ตัวอย่างเช่น source file ที่ทำงานในการโหลดค่า array อาจจะตั้งชื่อได้ดังนี้
array_linux.go | array_darwin.go | array_windows.go | array_freebsd.go
`go build` chooses the one that's associated with your operating system. For example, it only compiles array_linux.go in Linux systems, and ignores all the others.
`go build` จะเลือกไฟล์ที่ตรงกับระบบปฎิบัติการที่ใช้ ยกตัวอย่างเช่น ไฟล์ array_linux.go จะถูกคอมไฟล์ในระบบปฎิบัติการ Linux และไฟล์อื่นๆ จะถูกข้ามไป
## go clean
This command is for cleaning files that are generated by compilers, including the following files:
คำสั่งนี้ใช้ในการลบไฟล์ที่ถูกสร้างขึ้นมาโดยคอมไพเลอร์ ซึ่งรวมถึงทุกไฟล์ดังต่อไปนี้
_obj/ // old directory of object, left by Makefiles
_test/ // old directory of test, left by Makefiles
_testmain.go // old directory of gotest, left by Makefiles
test.out // old directory of test, left by Makefiles
build.out // old directory of test, left by Makefiles
*.[568ao] // object files, left by Makefiles
_obj/ // ไดเร็คทอรี่ object ที่เหลือจากการทำงานของ Makefile
_test/ // ไดเร็คทอรี่ test ที่เหลือจากการทำงานของ Makefile
_testmain.go // ไดเร็คทอรี่ gotest ที่เหลือจากการทำงานของ Makefile
test.out // ไดเร็คทอรี่ test ที่เหลือจากการทำงานของ Makefile
build.out // ไดเร็คทอรี่ test ที่เหลือจากการทำงานของ Makefile
*.[568ao] // ไฟล์ object ที่เหลือจากการทำงานของ Makefile
DIR(.exe) // generated by go build
DIR.test(.exe) // generated by go test -c
MAINFILE(.exe) // generated by go build MAINFILE.go
DIR(.exe) // สร้างจากคำสั่ง go build
DIR.test(.exe) // สร้างจากคำสั่ง go test -c
MAINFILE(.exe) // สร้างจากคำสั่ง go build MAINFILE.go
I usually use this command to clean up my files before I upload my project to Github. These are useful for local tests, but useless for version control.
โดยปรกติผมจะใช้คำสั่งนี้เพื่อลบไฟล์ที่ไม่ต้องการก่อนที่จะอัพโหลดโปรเจ็คไปที่ Github ซึ่งไฟล์เหล่านี้มีประโยชน์ในการทำงานบนเครื่อง แต่ไม่ได้มีประโยชน์กับ version control
## go fmt และ gofmt
The people who are working with C/C++ should know that people are always arguing about which code style is better: K&R-style or ANSI-style. However in Go, there is only one code style which is enforced. For example, left braces must only be inserted at the end of lines, and they cannot be on their own lines, otherwise you will get compile errors! Fortunately, you don't have to remember these rules. `go fmt` does this job for you. Just execute the command `go fmt <File name>.go` in terminal. I don't use this command very much because IDEs usually execute this command automatically when you save source files. I will talk more about IDEs in the next section.
สำหรับคนที่เคยใช้งาน C/C++ คงจะคุ้นเคยดีกับหัวข้อถกเถียงในเรื่องที่ว่าสไตล์ของ code แบบไหนที่ดีกว่ากัน ระหว่างสไตล์ K&R กับสไตล์ ANSI แต่อย่างไรก็ตาม สำหรับ Go แล้วจะมีสไตล์การเขียน code แบบเดียวที่ถูกกำหนดมา ยกตัวอย่างเช่น วงเล็บด้านซ้ายจะต้องจะต้องถูกใส่อยู่ท้ายบรรทัดเท่านั้น โดยไม่สามารถอยู่เดี่ยวๆ ในบรรทัดใดๆ ได้ ไม่เช่นนั้นก็จะเกิด error ขึ้นตอนคอมไพล์เลยทีเดียว และเป็นเรื่องโชคดีมากที่เราไม่ต้องมาเสียเวลานั่งจำกฎยิบย่อยเหล่านี้เอง `go fmt` จะทำงานนี้แทนเรา แค่สั่งคำสั่ง `go fmt <File name>.go` ใน terminal เท่านั้น ผมไม่ต้องใช้คำสั่งนี้บ่อยนักเพราะ IDE ต่างๆ
มักจะรันคำสั่งนี้โดยอัตโนมัติในตอนที่เราสั่งบันทึกไฟล์ โดยเราจะคุยกันเรื่อง IDE มากกว่านี้ในบทถัดไป
`go fmt` is just an alias, which runs the command 'gofmt -l -w' on the packages named by the import paths.
`go fmt` นั้นความจริงแล้วเป็นเพียง alias เท่านั้น ซึ่งเบื้องหลังจะเป็นการสั่งงานด้วย 'gofmt -l -w' ใน package ที่ใช้ชื่อตาม import path
We usually use `gofmt -w` instead of `go fmt`. The latter will not rewrite your source files after formatting code. `gofmt -w src` formats the whole project.
ปรกติเราจะใช้ `gofmt -w` มากกว่า `go fmt` โดยคำสั่งหลังนี้จะไม่เขียนทับไฟล์เดิมหลังทำการฟอร์แมต code แล้ว โดยการสั่ง `gofmt -w src` จะเป็นการสั่งฟอร์แมตทั้งโปรเจ็ค
## go get
This command is for getting remote packages. So far, it supports BitBucket, Github, Google Code and Launchpad. There are actually two things that happen after we execute this command. The first thing is that Go downloads the source code, then executes `go install`. Before you use this command, make sure you have installed all of the related tools.
คำสั่งนี้จะเป็นการดึง package มาใช้งานจากเครื่องอื่น โดยตั้งแต่ต้นนั้นจะ support การดึงมาจาก BitBucket, Github, Google Code และ Launchpad โดยจะเกิดการทำงานสองอย่างขึ้นจากการสั่งคำสั่งนี้ อย่างแรกคือ Go จะดาวน์โหลด source code มา เสร็จแล้วจะสั่ง `go install` ให้เองเลย แต่ก่อนที่เราจะใช้คำสั่งนี้ กรุณาตรวจสอบให้มั่นใจว่ามีการติดตั้งเครื่องมือที่เกี่ยวข้องไว้ก่อนแล้ว ดังนี้
BitBucket (Mercurial Git)
Github (git)
Google Code (Git, Mercurial, Subversion)
Launchpad (Bazaar)
In order to use this command, you have to install these tools correctly. Don't forget to update the `$PATH` variable. By the way, it also supports customized domain names. Use `go help importpath` for more details about this.
หากต้องการใช้คำสั่งนี้ เราต้องทำการติดตั้งเครื่องมือที่จำเป็นเหล่านี้ก่อน และอย่าลืมอัพเดทค่าตัวแปร `$PATH` ด้วย แต่อย่างไรก็ตาม คำสั่งนี้ก็ยัง support การใช้งานกับ domain name อื่นๆ โดยสามารถดูข้อมูลเพิ่มเติมได้จากคำสั่ง `go help importpath`
## go install
This command compiles all packages and generates files, then moves them to `$GOPATH/pkg` or `$GOPATH/bin`.
คำสั่งนี้จะเป็นการคอมไพล์ package ทุกอัน และทำการ generate ไฟล์ เสร็จแล้วย้ายไฟล์ที่ถูก generate ขึ้นมานี้ไปไว้ที่ `$GOPATH/pkg` หรือ `$GOPATH/bin`
## go test
This command loads all files whose name include `*_test.go` and generates test files, then prints information that looks like the following.
คำสั่งนี้จะเป็นการโหลดไฟล์ทุกอันที่ชื่อไฟล์ลงท้ายด้วย `*_test.go` และ generate ไฟล์ test และเมื่อทำงานเสร็จแล้วจะได้ผลลัพธ์ที่หน้าจอที่ดูเหมือนตัวอย่างนี้
ok archive/tar 0.011s
FAIL archive/zip 0.022s
ok compress/gzip 0.033s
...
It tests all your test files by default. Use command `go help testflag` for more details.
โดยปรกติมันจะทำการสั่งรัน tests นั่นเอง โดยสามารถดูรายละเอียดเพิ่มได้โดยใช้คำสั่ง `go help testflag`
## godoc
Many people say that we don't need any third-party documentation for programming in Go (actually I've made a [CHM](https://github.com/astaxie/godoc) already). Go has a powerful tool to manage documentation natively.
หลายคนพูดว่าเราไม่ต้องการเอกสารเพิ่มเติมจากแหล่งอื่นๆ ในการใช้งานภาษา Go (จริงๆ แล้วผมได้สร้าง [CHM](https://github.com/astaxie/godoc) ไว้แล้ว) เพราะ Go นั้นมาพร้อมกับเอกสารและเครื่องมือที่ทรงพลังอยู่แล้ว
So how do we look up package information in documentation? For instance, if you want to get more details about the `builtin` package, use the `godoc builtin` command. Similarly, use the `godoc net/http` command to look up the `http` package documentation. If you want to see more details about specific functions, use the `godoc fmt Printf` and `godoc -src fmt Printf` commands to view the source code.
แล้วการเรียกดูข้อมูลของ package ในเอกสารนั้น ต้องทำอย่างไรหละ? อย่างเช่น หากเราต้องการหาข้อมูลของ package ประเภท `builtin` ต่างๆ เราสามารถใช้คำสั่ง `godoc builtin` และก็เหมือนกันกับคำสั่ง `godoc net/http` จะเป็นการเรียกดูเอกสารของ package `http` นั่นเอง หากเราต้องการดูรายละเอียดที่เจาะจงลงไปที่ระดับ function สามารถใช้คำสั่ง `go fmt Printf` และใช้คำสั่ง `godoc -src fmt` หากต้องการดู source code
Execute the `godoc -http=:8080` command, then open `127.0.0.1:8080` in your browser. You should see a localized golang.org. It can not only show the standard packages' information, but also packages in your `$GOPATH/pkg`. It's great for people who are suffering from the Great Firewall of China.
สั่งคำสั่ง `godoc -http=:8080` แล้วเปิดเบราเซอร์ไปที่ `127.0.0.1:8080` เราจะสามารถดูเอกสารของ golang.org บนจากบนเครื่องของเราเองได้ แต่มีข้อมูลเฉพาะ package มาตรฐานเท่านั้น แต่ก็จะมีของ package อื่นที่อยู่ใน `$GOPATH/pkg` ด้วย ซึ่งเป็นสิ่งที่เยี่ยมมากสำหรับคนที่เจ็บปวดกับ Great Firewall ของจีน
## คำสั่งอื่นๆ
Go provides more commands than those we've just talked about.
นอกจากคำสั่งต่างๆ ที่กล่าวมาแล้ว Go ยังมีคำสั่งอื่นๆ อีก ดังนี้
go fix // upgrade code from an old version before go1 to a new version after go1
go version // get information about your version of Go
go env // view environment variables about Go
go list // list all installed packages
go run // compile temporary files and run the application
go fix // อัพเกรด code จากเวอร์ชั่นเก่าก่อน go1 ไปเป็นเวอร์ชั่นใหม่ซึ่งใหม่กว่า go1
go version // เรียกดูข้อมูลเวอร์ชั่นของ Go
go env // เรียกดูข้อมูลตัวแปร environment ที่เกี่ยวข้องกับ Go
go list // เรียกดูรายชื่อ package ที่ทำการติดตั้งไปแล้ว
go run // รัน application โดยการคอมไพล์เป็นไฟล์ชั่วคราวก่อน
There are also more details about the commands that I've talked about. You can use `go help <command>` to look them up.
โดยคำสั่งที่ได้กล่าวมานี้ ยังมีรายละเอียดการใช้งานเพิ่มเติมอื่นๆ อีกมากมาย โดยเราจะสามารถเรียกดูได้โดยใช้คำสั่ง `go help <command>`
## Links