安装:
hg clone -u release https://code.google.com/p/go cd go/src ./all.bash
下载编译好的安装包
http://code.google.com/p/go/downloads/list
编辑器:
liteIDE http://code.google.com/p/golangide/downloads/list
Eclipse --- goeclipse插件
http://code.google.com/p/goclipse/wiki/InstallationInstructions
代码提示+自动完成
go get -u github.com/nsf/gocode查看源文件可以使用NotePad++
基本命令:
go build test clean fmt get install doc
编译 测试 清理 格式化 获取远程代码 安装 说明
语言基础:
基础类型:
布尔 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