Install on Ubuntu
- 至 https://golang.org/dl/ 下載最新的 golang.tar
tar xvf golang.tar
sudo chown -R root:root ./go
sudo mv go /usr/local
echo "export GOPATH=$HOME/work" >> ~/.bashrc
echo "export PATH=$PATH:/usr/local/go/bin:$GOPATH/bin" >> ~/.bashrc
mkdir $HOME/work
VSCODE 設定
- 參考 http://www.evanlin.com/dive-with-vscode-golang/
- 參考 https://github.com/Microsoft/vscode-go/wiki/Debugging-Go-code-using-VS-Code
參考書
- http://ithelp.ithome.com.tw/users/20079210/ironman/721
- http://ithelp.ithome.com.tw/articles/10156046 //IT邦幫忙教學
- https://gobyexample.com/hello-world
- http://cepave.com/http-restful-api-with-golang/
- https://golang.org/pkg/net/http/ //原廠Golang教學
- http://docs.plhwin.com/gopl-zh/ //Go语言圣经(中文版)
- http://www.01happy.com/golang-os-exec/ //介紹 os/exec模組
- http://www.gopl.io/
- https://astaxie.gitbooks.io/build-web-application-with-golang/content/zh
- https://golang.org/doc/code.html
- http://hustcat.github.io/tags/#go
- https://wizardforcel.gitbooks.io/w3school-go/content/10.html
保留字
- break default func interface select
- case defer go map struct
- chan else goto package switch
- const fallthrough if range type
- continue for import return var
變數宣告與命名方式
- 單字變數:數值、字串、布林、常數
var a string
//宣告變數a為string型態var b int = 66
//宣告變數b為整數型態,並賦值66- 常數
const Pi = 3.14159
//常數宣告
數組變數:陣列、切面、地圖
- array
- slice
- map
宣告變數排版
- `const (
)`Pi = 3.14159
- `var (
)`a = "Sam" b string = "Marc" c = 12
- `import(
)`"fmt" "os" "os/exec"
- 簡短宣告(僅用於函式內)
s := 0
//只宣告值,型態會自動判斷 intf, err := os.Open(name)
//同時宣告f與err兩個變數,並賦值f=os.Open(name)與err=""
- 完整宣告
var s string
//只宣告型態,值為預設 ""var s = "your name"
//只宣告值,型態會自動判斷 string- `var s string = " your name" //宣告型態與值
- `const (
條件式與迴圈
- for 迴圈 //Go語言僅提供for迴圈可取代 while 與 until-do
-`for 起始值; 條件式; 迴圈結束後執行 {
}`// zero or more statements
- if 條件式 //
- `if x > 0 {
}`return y
- `if x > 0 {
- switch 條件式 //
- `switch marks {
case 90:
case 80:grade = "A"
case 50, 60, 70:grade = "B"
default:grade = "C"
}`grade = "D"
- `switch marks {
case 90:
- select 條件式
- `select {
}`case communication clause : statement(s); case communication clause : statement(s); default : statement(s);
- `select {
- goto 條件式 //強制前往 ( 少用)
常用函數
- fmt
fmt.Print()
//印出變數於螢幕無段落fmt.Println()
//印出變數於螢幕並段落(Enter)
- log
log.Printf("Hello")
log.Fatal(err)
- os/exec
exec.Command()
cmd.Start()
cmd.Run()
cmd.Wait()