安裝: git
hg clone -u release https://code.google.com/p/go cd go/src ./all.bash
下載編譯好的安裝包 github
http://code.google.com/p/go/downloads/list golang
編輯器: 數組
liteIDE http://code.google.com/p/golangide/downloads/list bash
Eclipse --- goeclipse插件 併發
http://code.google.com/p/goclipse/wiki/InstallationInstructions
代碼提示+自動完成 app
go get -u github.com/nsf/gocode查看源文件可使用NotePad++
基本命令: eclipse
go build test clean fmt get install doc 編輯器
編譯 測試 清理 格式化 獲取遠程代碼 安裝 說明 ide
語言基礎:
基礎類型:
布爾 bool 整型(int8-byte int16 int32-rune int64 uint8 uint16 uint32 uint64) int uint 字符串 string 錯誤 error
常量 const
表達式:
const PI = 3.141592653
var i int = 1
i := 1
枚舉 itoa
const( x = iota // x == 0 y = iota // y == 1 z = iota // z == 2 w // 常量聲明省略值時,默認和以前一個值的字面相同。這裏隱式地說w = iota,所以w == 3。其實上面y和z可一樣不用"= iota" )
array 數組 slice 不需聲明長度的數組 map
a := [3]int{1, 2, 3} c := [...]int{4, 5, 6} slice := []byte {'a', 'b', 'c', 'd'} // 聲明一個數組 var array = [10]byte{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'} // 聲明兩個slice var aSlice, bSlice []byte // 演示一些簡便操做 aSlice = array[:3] // 等價於aSlice = array[0:3] aSlice包含元素: a,b,c aSlice = array[5:] // 等價於aSlice = array[5:9] aSlice包含元素: f,g,h,i,j aSlice = array[:] // 等價於aSlice = array[0:9] 這樣aSlice包含了所有的元素 // 從slice中獲取slice aSlice = array[3:7] // aSlice包含元素: d,e,f,g,len=4,cap=7 bSlice = aSlice[1:3] // bSlice 包含aSlice[1], aSlice[2] 也就是含有: e,f bSlice = aSlice[:3] // bSlice 包含 aSlice[0], aSlice[1], aSlice[2] 也就是含有: d,e,f bSlice = aSlice[0:5] // 對slice的slice能夠在cap範圍內擴展,此時bSlice包含:d,e,f,g,h bSlice = aSlice[:] // bSlice包含全部aSlice的元素: d,e,f,g // 聲明一個key是字符串,值爲int的字典,這種方式的聲明須要在使用以前使用make初始化 var numbers map[string] int // 另外一種map的聲明方式 numbers := make(map[string]int) numbers["one"] = 1 //賦值 numbers["ten"] = 10 //賦值 numbers["three"] = 3make 和 new
make用於內建類型(map、slice 和channel)的內存分配。new用於各類類型的內存分配
new返回指針 make返回初始化後的(非零)值
流程函數:
if goto for switch
func funcName(input1 type1, input2 type2) (output1 type1, output2 type2) { //這裏是處理邏輯代碼 //返回多個值 return value1, value2 }變參支持 --- 採用array實現
支持傳遞指針 * &
defer 關鍵字,在函數退出以後執行,後進先出的棧原則
struct:
struct支持匿名字段
package main import "fmt" type Human struct { name string age int phone string // Human類型擁有的字段 } type Employee struct { Human // 匿名字段Human speciality string phone string // 僱員的phone字段 } func main() { Bob := Employee{Human{"Bob", 34, "777-444-XXXX"}, "Designer", "333-222"} fmt.Println("Bob's work phone is:", Bob.phone) // 若是咱們要訪問Human的phone字段 fmt.Println("Bob's personal phone is:", Bob.Human.phone) }面向對象:
func (r ReceiverType) funcName(parameters) (results)能夠繼承和重載
interface
package main import "fmt" type Human struct { name string age int phone string } type Student struct { Human //匿名字段 school string loan float32 } type Employee struct { Human //匿名字段 company string money float32 } //Human實現Sayhi方法 func (h Human) SayHi() { fmt.Printf("Hi, I am %s you can call me on %s\n", h.name, h.phone) } //Human實現Sing方法 func (h Human) Sing(lyrics string) { fmt.Println("La la la la...", lyrics) } //Employee重載Human的SayHi方法 func (e Employee) SayHi() { fmt.Printf("Hi, I am %s, I work at %s. Call me on %s\n", e.name, e.company, e.phone) //Yes you can split into 2 lines here. } // Interface Men被Human,Student和Employee實現 // 由於這三個類型都實現了這兩個方法 type Men interface { SayHi() Sing(lyrics string) } func main() { mike := Student{Human{"Mike", 25, "222-222-XXX"}, "MIT", 0.00} paul := Student{Human{"Paul", 26, "111-222-XXX"}, "Harvard", 100} sam := Employee{Human{"Sam", 36, "444-222-XXX"}, "Golang Inc.", 1000} Tom := Employee{Human{"Sam", 36, "444-222-XXX"}, "Things Ltd.", 5000} //定義Men類型的變量i var i Men //i能存儲Student i = mike fmt.Println("This is Mike, a Student:") i.SayHi() i.Sing("November rain") //i也能存儲Employee i = Tom fmt.Println("This is Tom, an Employee:") i.SayHi() i.Sing("Born to be wild") //定義了slice Men fmt.Println("Let's use a slice of Men and see what happens") x := make([]Men, 3) //T這三個都是不一樣類型的元素,可是他們實現了interface同一個接口 x[0], x[1], x[2] = paul, sam, mike for _, value := range x{ value.SayHi() } }併發:
go chan select
ch := make(chan type, value) value == 0 ! 無緩衝(阻塞) value > 0 ! 緩衝(非阻塞,直到value 個元素)
package main import "fmt" type request struct { a, b int replyc chan int } type binOp func(a, b int) int func run(op binOp, req *request) { reply := op(req.a, req.b) req.replyc <- reply } func server(op binOp, service chan *request, quit chan bool) { for { select { case req := <-service: go run(op, req) // don't wait for it case ok := <-quit: fmt.Println(ok) return } } } func startServer(op binOp) (service chan *request, quit chan bool) { service = make(chan *request) quit = make(chan bool) go server(op, service, quit) return service, quit } func test(a, b int) (c int) { c = a + b fmt.Println(c) return c } func main() { adder, quit := startServer(test) const N = 100 var reqs [N]request for i := 0; i < N; i++ { req := &reqs[i] req.a = i req.b = i + N req.replyc = make(chan int) adder <- req } quit <- true for i := N - 1; i >= 0; i-- { // doesn't matter what order if <-reqs[i].replyc != N+2*i { fmt.Println("fail at", i) } } fmt.Println("done") }關鍵字:
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