├── .struct2.go.swp ├── README.md ├── anonymousfields1.go ├── anonymousfields2.go ├── anonymousfields3.go ├── channels1.go ├── channels2.go ├── channels3.go ├── channels4.go ├── channels5.go ├── forloop1.go ├── forloop2.go ├── forloop3.go ├── forloop4.go ├── forloop5.go ├── forloop6.go ├── forloop7.go ├── goerror1.go ├── goerror2.go ├── goerror3.go ├── goerror4.go ├── goroutines1.go ├── goroutines2.go ├── helloworld.go ├── ifelse1.go ├── ifelse2.go ├── inherit1.go ├── inherit2.go ├── inherit3.go ├── interface1.go ├── interface2.go ├── interface3.go ├── interface4.go ├── interface5.go ├── multipleinheritance.go ├── polymorphism1.go ├── polymorphism2.go ├── shortit.go ├── struct1.go ├── struct2.go ├── structsmethods1.go ├── structsmethods2.go ├── structsmethods3.go ├── structsmethods4.go ├── structsmethods5.go ├── structsmethods6.go ├── t1.tmpl ├── t2.tmpl ├── templates1.go ├── templates2.go ├── templates3.go ├── templates4.go ├── templates5.go ├── templates6.go ├── templates7.go ├── templates8.go ├── templates9.go ├── web1.go └── web2.go /.struct2.go.swp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/binque/golang-examples/1115e54a8fc3f4f45afccb6461927855ad7e6962/.struct2.go.swp -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Golang 辅助手册 / Golang Tutorials 2 | 3 | Golang 辅助手册是我在自己的学习过程中查阅资料的总结以及一些自己的想法的文章合集,它们或是自己的总结,或是别人优秀的入门教程的翻译,希望对你的Golang入门有所帮助,另外,有任何错误或者兴合理的内容还希望你能在第一时间[告诉我](/contact.html)。 4 | 5 | 本大纲中很多内容都翻译自[Golang Tutorials](http://golangtutorials.blogspot.com/),原网站不能访问。 6 | 7 | ## Golang辅助手册内容大纲 8 | 9 | ### 第一章节:Go的安装与配置 10 | 11 | + [安装 Golang 二进制发行包](http://cox.im/articles/install-golang-from-a-binary-distribution.html) 12 | 13 | ### 第二章节:Go语言入门与基本概念 14 | 15 | + [通过一行一行的代码写出Go的Hello World程序](http://cox.im/articles/line-by-line-go-hello-world.html) 16 | + [Go的Hello World程序](http://cox.im/articles/go-hello-world.html) 17 | + [写 GO 程序中一些常见的语法和其它错误](http://cox.im/articles/early-syntax-errors-and-other-minor-errors-in-go.html) 18 | + [如何写 Go 程序](http://cox.im/articles/how-to-write-go-code.html) 19 | + [构建高效率的 Go 程序(Effective Go)](http://cox.im/articles/effective-go.html) 20 | 21 | ### 第三章节:Go数据结构及语法规则 22 | 23 | + [Go 中的 for 循环控制结构](http://cox.im/articles/for-loop-control-structures-in-go.html) 24 | + [Go 的 If Else 声明控制结构](http://cox.im/articles/go-if-else-control-structures.html) 25 | 26 | ### 第四章节 Go的面向对象编程(Struct) 27 | 28 | + [Go 中的 Struct——无类实现面向对象编程](http://cox.im/articles/object-oriented-programming-in-go-via-structs-instead-of-classes.html) 29 | + [Go Struct中的匿名字段](http://cox.im/articles/anonymous-fields-in-go-structs.html) 30 | + [Go Structs 中的方法](http://cox.im/articles/methods-on-structs-in-go.html) 31 | + [Go 中的继承与子类——或者类似的性质](http://cox.im/articles/inheritance-and-subclassing-in-go.html) 32 | + [Go 的多继承](http://cox.im/articles/multiple-inheritance-in-go.html) 33 | + [Go 中的接口](http://cox.im/articles/interfaces-in-go.html) 34 | + [Go 的接口 —— 进化与高可用性的设计](http://cox.im/articles/interfaces-in-go-adaptable.html) 35 | + [Go 的多态](http://cox.im/articles/polymorphism-in-go.html) 36 | 37 | ### 第五章节 并行(Goroutines 与 Channels) 38 | 39 | + [Go 的 Goroutines](http://cox.im/articles/goroutines-in-go.html) 40 | + [Go 的 Channels](http://cox.im/articles/channels-in-go.html) 41 | + [Go 的 Channels —— range 与 select](http://cox.im/articles/channels-in-go-range-and-select.html) 42 | 43 | ### 第六章节 网络编程(Web Programming) 44 | 45 | + [网络编程的 Hello World程序](http://cox.im/articles/hello-world-for-web-programming-in-go.html) 46 | + [Go 模板](http://cox.im/articles/go-templates.html) 47 | + [Go 模板——结构与数据控制](http://cox.im/articles/go-templates-structure-and-data-control.html) 48 | + [Go 模板集](http://cox.im/articles/go-templates-sets.html) 49 | + [如何基于Go创建数据库驱动的Web应用](http://cox.im/articles/how-to-write-database-driven-web-application-using-go.html) 50 | + [在Go网络编程中使用外部API——基于 Google API 创建URL地址缩短服务](http://cox.im/articles/using-external-api-in-go-web-program-urlshortener-from-google-api.html) 51 | -------------------------------------------------------------------------------- /anonymousfields1.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | type Kitchen struct { 4 | numOfPlates int 5 | } 6 | type House struct { 7 | Kitchen // 匿名字段 8 | numOfRooms int 9 | } 10 | func main() { 11 | h := House{Kitchen{10}, 3} // 初始化需要使用Struct名 12 | fmt.Println("房屋 h 有", h.numOfRooms, "间房") // numOfRooms 是 House 的一个字段 13 | fmt.Println("房屋 h 有", h.numOfPlates, "个盘子") // numOfPlates 是Kitchen提供的字段, 14 | //而Kitchen又是House的一个匿名字段,所以这里可以访问到它 15 | fmt.Println("这间房屋的厨房有:", h.Kitchen) // 我们可以通过Struct的名称访问整个匿名Struct的内容 16 | } 17 | -------------------------------------------------------------------------------- /anonymousfields2.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | type Kitchen struct { 4 | numOfLamps int 5 | } 6 | type House struct { 7 | Kitchen 8 | numOfLamps int 9 | } 10 | func main() { 11 | h := House{Kitchen{2}, 10} // Kitchen 有2个Lamps,House有10个 12 | fmt.Println("House有", h.numOfLamps, "个Lamps") 13 | fmt.Println("Kitchen有", h.Kitchen.numOfLamps, "个Lamps") 14 | } 15 | -------------------------------------------------------------------------------- /anonymousfields3.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | type Kitchen struct { 4 | numOfLamps int; 5 | } 6 | type Bedroom struct { 7 | numOfLamps int; 8 | } 9 | type House struct { 10 | Kitchen 11 | Bedroom 12 | } 13 | func main() { 14 | h := House{Kitchen{2}, Bedroom{3}} // Kitchen 2, Bedroom 3 15 | fmt.Println("Ambiguous number of lamps:", h.numOfLamps) // 这会出错 16 | } 17 | -------------------------------------------------------------------------------- /channels1.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | "strconv" 7 | ) 8 | 9 | var i int 10 | 11 | func makeCakeAndSend(cs chan string) { 12 | i = i + 1 13 | cakeName := "Strawberry Cake " + strconv.Itoa(i) 14 | fmt.Println("Making a cake and sending...", cakeName) 15 | cs <- cakeName // send a strawberry cake 16 | } 17 | 18 | func receiveCakeAndPack(cs chan string) { 19 | s := <- cs // get whatever cake is on the channel 20 | fmt.Println("Packing received cake: ", s) 21 | } 22 | 23 | func main() { 24 | cs := make(chan string) 25 | for i := 1; i < 5; i++ { 26 | go makeCakeAndSend(cs) 27 | go receiveCakeAndPack(cs) 28 | } 29 | // Sleep for a while so that the program doesn't exit immediately and output is clear for illustration 30 | wait, _ := time.ParseDuration("2s") 31 | time.Sleep(wait) 32 | } 33 | -------------------------------------------------------------------------------- /channels2.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | "strconv" 7 | ) 8 | 9 | func makeCakeAndSend(cs chan string) { 10 | for i := 1; i<=3; i++ { 11 | cakeName := "Strawberry Cake " + strconv.Itoa(i) 12 | fmt.Println("Making a cake and sending...", cakeName) 13 | cs <- cakeName 14 | } 15 | } 16 | 17 | func receiveCakeAndPack(cs chan string) { 18 | for i := 1; i<=3; i++ { 19 | s := <- cs 20 | fmt.Println("Packing received cake: ", s) 21 | } 22 | } 23 | 24 | func main() { 25 | cs := make(chan string) 26 | go makeCakeAndSend(cs) 27 | go receiveCakeAndPack(cs) 28 | 29 | wait, _ := time.ParseDuration("5s") 30 | time.Sleep(wait) 31 | } 32 | -------------------------------------------------------------------------------- /channels3.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | "strconv" 7 | ) 8 | 9 | func makeCakeAndSend(cs chan string, count int) { 10 | for i := 1; i <= count; i++ { 11 | cakeName := "Strawberry Cake " + strconv.Itoa(i) 12 | cs <- cakeName // 传递一个 cake 13 | } 14 | } 15 | 16 | func receiveCakeAndPack(cs chan string) { 17 | for s := range cs { 18 | fmt.Println("Packing received cake: ", s) 19 | } 20 | } 21 | 22 | func main() { 23 | cs := make(chan string) 24 | go makeCakeAndSend(cs, 5) 25 | go receiveCakeAndPack(cs) 26 | 27 | du,_ := time.ParseDuration("3s") 28 | time.Sleep(du) 29 | } 30 | -------------------------------------------------------------------------------- /channels4.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | "strconv" 7 | ) 8 | 9 | func makeCakeAndSend(cs chan string, flavor string, count int) { 10 | for i := 1; i <= count; i++ { 11 | cakeName := flavor + " Cake " + strconv.Itoa(i) 12 | cs <- cakeName // 传递一个 cake 13 | } 14 | } 15 | 16 | func receiveCakeAndPack(cs chan string) { 17 | for s := range cs { 18 | fmt.Println("Packing received cake: ", s) 19 | } 20 | } 21 | 22 | func main() { 23 | cs := make(chan string) 24 | go makeCakeAndSend(cs, "Strawberry", 5) 25 | go makeCakeAndSend(cs, "Chocolate", 10) 26 | go receiveCakeAndPack(cs) 27 | 28 | du,_ := time.ParseDuration("2s") 29 | time.Sleep(du) 30 | } 31 | -------------------------------------------------------------------------------- /channels5.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "time" 6 | "strconv" 7 | ) 8 | 9 | func makeCakeAndSend(cs chan string, flavor string, count int) { 10 | for i := 1; i <= count; i++ { 11 | cakeName := flavor + " Cake " + strconv.Itoa(i) 12 | cs <- cakeName 13 | } 14 | } 15 | 16 | func receiveCakeAndPack(strbry_cs chan string, choco_cs chan string) { 17 | strbry_closed, choco_closed := false, false 18 | for { 19 | if (strbry_closed && choco_closed) { return } 20 | fmt.Println("Waiting for a new cake ...") 21 | select { 22 | case cakeName, strbry_ok := <- strbry_cs: 23 | if (!strbry_ok) { 24 | strbry_closed = true 25 | fmt.Println("... Strawberry channel closed!") 26 | } else { 27 | fmt.Println("Received from Strawberry channel. Now packing ", cakeName) 28 | } 29 | case cakeName, choco_ok := <- choco_cs: 30 | if (!choco_ok) { 31 | choco_closed = true 32 | fmt.Println("... Chocolate channel closed!") 33 | } else { 34 | fmt.Println("Received from Chocolate channel. Now packing ", cakeName) 35 | } 36 | } 37 | } 38 | } 39 | 40 | func main() { 41 | scs := make(chan string) 42 | ccs := make(chan string) 43 | go makeCakeAndSend(scs, "Strawberry", 3) 44 | go makeCakeAndSend(ccs, "Chocolate", 3) 45 | go receiveCakeAndPack(scs,ccs) 46 | du, _ := time.ParseDuration("3s") 47 | time.Sleep(du) 48 | } 49 | -------------------------------------------------------------------------------- /forloop1.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | func main() { 4 | // 初始化i为0;每一次循环之后检查i是否小于5;让i加1 5 | for i := 0; i < 5; i++ { 6 | fmt.Println("i现在的值为:", i) 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /forloop2.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | func main() { 4 | // for i := 0; ; i++ { 5 | // fmt.Println("i现在的值为:", i) 6 | // } 7 | // for i := 0; i < 3; { 8 | // fmt.Println("i现在的值为", i) 9 | // } 10 | s := "" 11 | for ; s != "aaaaa"; { 12 | fmt.Println("s的值为:", s) 13 | s = s + "a" 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /forloop3.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | func main() { 4 | for i, j, s := 0,5,"a"; i < 3 && j < 100 && s != "aaaaa"; i, j, s = i+1, j+1, s + "a" { 5 | fmt.Printf("i=%d, j=%d, s=%s\n", i, j, s) 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /forloop4.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | func main() { 4 | i := 0 5 | for { 6 | if i >= 3 { break } 7 | fmt.Println("当前i的值为:", i) 8 | i++ 9 | } 10 | fmt.Println("for后的第一个声明") 11 | } 12 | -------------------------------------------------------------------------------- /forloop5.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | func main() { 4 | for i := 0; i < 11; i++ { 5 | if i%2 == 0 { 6 | continue //我不输入偶数 7 | } 8 | fmt.Println("当前i的值为:", i) 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /forloop6.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | func main() { 4 | // 对于数组,range 返回数据的索引 5 | a := [...]string{"a", "b", "c", "d"} 6 | for i := range a { 7 | fmt.Println("数组的第", i+1, "个值为:", a[i]) 8 | } 9 | 10 | // 对于 map,返回每一个键 11 | capitals := map[string]string {"France":"Paris", "Italy":"Rome", "China":"Beijing" } 12 | for key := range capitals { 13 | fmt.Println("Capital of", key, "is", capitals[key]) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /forloop7.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | func main() { 4 | // 对于数组,range 返回数据的索引 5 | a := [...]string{"a", "b", "c", "d"} 6 | for i,v := range a { 7 | fmt.Println("数组的第", i+1, "个值为:", v) 8 | } 9 | 10 | // 对于 map,返回每一个键 11 | capitals := map[string]string {"France":"Paris", "Italy":"Rome", "China":"Beijing" } 12 | for key,value := range capitals { 13 | fmt.Println("Capital of", key, "is", value) 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /goerror1.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | import "os" 5 | 6 | func main() { 7 | fmt.Println("Hello World!") 8 | } 9 | -------------------------------------------------------------------------------- /goerror2.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.println("Hello World!") 7 | } 8 | -------------------------------------------------------------------------------- /goerror3.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("Hello"); fmt.Println("World!"); 7 | } 8 | -------------------------------------------------------------------------------- /goerror4.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt";; 4 | 5 | func main() { 6 | fmt.Print("Hello World!") 7 | } 8 | -------------------------------------------------------------------------------- /goroutines1.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | func LoopIt(times int) { 4 | for i := 0; i < times; i++ { 5 | fmt.Printf("%d\t", i) 6 | } 7 | fmt.Println("--Loop End--") 8 | } 9 | func main() { 10 | go LoopIt(10) 11 | LoopIt(10) 12 | } 13 | -------------------------------------------------------------------------------- /goroutines2.go: -------------------------------------------------------------------------------- 1 | package main 2 | import ( 3 | "fmt" 4 | "time" 5 | ) 6 | func simulateEvent(name string, timeInSecs string) { 7 | fmt.Println("Started ", name, ": Should take", timeInSecs, "seconds") 8 | du, _ := time.ParseDuration(timeInSecs) 9 | time.Sleep(du) 10 | fmt.Println("Finished ", name) 11 | } 12 | func main() { 13 | go simulateEvent("First", "10s") 14 | go simulateEvent("Second", "6s") 15 | go simulateEvent("Third", "3s") 16 | wait, _ := time.ParseDuration("15s") 17 | time.Sleep(wait) 18 | fmt.Println("Exited") 19 | } 20 | -------------------------------------------------------------------------------- /helloworld.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "fmt" 4 | 5 | func main() { 6 | fmt.Println("Hello World! 您好!世界。") 7 | } 8 | -------------------------------------------------------------------------------- /ifelse1.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | func main() { 4 | if true { 5 | fmt.Println("这里会执行") 6 | } 7 | if false { 8 | fmt.Println("这里不会执行") 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /ifelse2.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | func main() { 4 | a, b := 4, 5 5 | if a < b { 6 | fmt.Println("a < b") 7 | } else if a > b { 8 | fmt.Println("a > b") 9 | } else { 10 | fmt.Println("a = b") 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /inherit1.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | type Car struct { 4 | wheelCount int 5 | } 6 | // 定义 Car 的行为 7 | func (car Car) numberOfWheels() int { 8 | return car.wheelCount 9 | } 10 | type Ferrari struct { 11 | Car // 匿名字段 Car 12 | } 13 | func main() { 14 | f := Ferrari{Car{4}} 15 | fmt.Printf("一辆法拉利有 %d 个轮胎\n", f.numberOfWheels()) 16 | } 17 | -------------------------------------------------------------------------------- /inherit2.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | type Car struct { 4 | wheelCount int 5 | } 6 | // 定义 Car 的行为 7 | func (car Car) numberOfWheels() int { 8 | return car.wheelCount 9 | } 10 | type Ferrari struct { 11 | Car // 匿名字段 Car 12 | } 13 | // 定义只有 Ferrari 专属的方法 14 | func (f Ferrari) sayHiToSchumacher() { 15 | fmt.Println("Hi Schumacher!") 16 | } 17 | type AstonMartin struct { 18 | Car 19 | } 20 | //添加只有 AstonMartin 专属的方法 21 | func (a AstonMartin) sayHiToBond() { 22 | fmt.Println("Hi Bond, James Bond!") 23 | } 24 | func main() { 25 | f := Ferrari{Car{4}} 26 | fmt.Printf("一辆法拉利有 %d 个轮胎\n", f.numberOfWheels()) 27 | f.sayHiToSchumacher() 28 | a := AstonMartin{Car{4}} 29 | fmt.Printf("一辆阿斯顿马丁有 %d 个轮胎\n", a.numberOfWheels()) 30 | a.sayHiToBond() 31 | } 32 | -------------------------------------------------------------------------------- /inherit3.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | type Car struct { 4 | wheelCount int 5 | } 6 | // 定义 Car 的行为 7 | func (car Car) numberOfWheels() int { 8 | return car.wheelCount 9 | } 10 | type Ferrari struct { 11 | Car // 匿名字段 Car 12 | } 13 | // 定义只有 Ferrari 专属的方法 14 | func (f Ferrari) sayHiToSchumacher() { 15 | fmt.Println("Hi Schumacher!") 16 | } 17 | type AstonMartin struct { 18 | Car 19 | } 20 | //添加只有 AstonMartin 专属的方法 21 | func (a AstonMartin) sayHiToBond() { 22 | fmt.Println("Hi Bond, James Bond!") 23 | } 24 | func main() { 25 | f := Ferrari{Car{4}} 26 | fmt.Printf("一辆法拉利有 %d 个轮胎\n", f.numberOfWheels()) 27 | f.sayHiToBond() 28 | a := AstonMartin{Car{4}} 29 | fmt.Printf("一辆阿斯顿马丁有 %d 个轮胎\n", a.numberOfWheels()) 30 | a.sayHiToSchumacher() 31 | } 32 | -------------------------------------------------------------------------------- /interface1.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | // Shaper是一个接口,它只有一个函数 Area返回一个int类型的结果 4 | type Shaper interface { 5 | Area() int 6 | } 7 | type Rectangle struct { 8 | length, width int 9 | } 10 | // 该 Area 函数工作于 Rectangle 类型,同时与 Shper有同样的定义,现在就称为Rectangle实现了Shaper接口 11 | func (r Rectangle) Area() int { 12 | return r.length * r.width 13 | } 14 | func main() { 15 | r := Rectangle{length: 5, width: 3} 16 | fmt.Println("Rectangle r details are: ", r) 17 | fmt.Println("Rectangle r's area is: ", r.Area()) 18 | s := Shaper(r) 19 | fmt.Println("Area of the Shape r is: ", s.Area()) 20 | } 21 | -------------------------------------------------------------------------------- /interface2.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | // Shaper是一个接口,它只有一个函数 Area返回一个int类型的结果 4 | type Shaper interface { 5 | Area() int 6 | } 7 | type Rectangle struct { 8 | length, width int 9 | } 10 | // 该 Area 函数工作于 Rectangle 类型,同时与 Shper有同样的定义,现在就称为Rectangle实现了Shaper接口 11 | func (r Rectangle) Area() int { 12 | return r.length * r.width 13 | } 14 | type Square struct { 15 | side int 16 | } 17 | func (sq Square) Area() int { 18 | return sq.side * sq.side 19 | } 20 | func main() { 21 | r := Rectangle{length: 5, width: 3} 22 | fmt.Println("Rectangle r details are: ", r) 23 | fmt.Println("Rectangle r's area is: ", r.Area()) 24 | s := Shaper(r) 25 | fmt.Println("Area of the Shape r is: ", s.Area()) 26 | sq := Square{5} 27 | fmt.Println("Square details is: ", sq) 28 | fmt.Println("Area of square sq is: ", sq.Area()) 29 | sqs := Shaper(sq) 30 | fmt.Println("Shaper sqs area is: ", sqs.Area()) 31 | } 32 | -------------------------------------------------------------------------------- /interface3.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | // Shaper是一个接口,它只有一个函数 Area返回一个int类型的结果 4 | type Shaper interface { 5 | Area() int 6 | } 7 | type Rectangle struct { 8 | length, width int 9 | } 10 | // 该 Area 函数工作于 Rectangle 类型,同时与 Shper有同样的定义,现在就称为Rectangle实现了Shaper接口 11 | func (r Rectangle) Area() int { 12 | return r.length * r.width 13 | } 14 | type Square struct { 15 | side int 16 | } 17 | func (sq Square) Area() int { 18 | return sq.side * sq.side 19 | } 20 | func main() { 21 | r := Rectangle{length: 5, width: 3} 22 | q := Square{side:5} 23 | shapes := [...]Shaper{r, q} 24 | fmt.Println("Looping through shapes for area ...") 25 | for n, _ := range shapes { 26 | fmt.Printf("Shape detail: %v , and is area is: %d\n", shapes[n], shapes[n].Area()) 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /interface4.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | // Go第一步:定义你的数据结构 4 | type Bus struct { 5 | l, b, h int 6 | rows, seatsPerRow int 7 | } 8 | // Go第二步:定义一个现实世界对象的抽象层 9 | type Cuboider interface { 10 | CubicVolume() int 11 | } 12 | // Go第三步:实现接口的方法 13 | func (bus Bus) CubicVolume() int { 14 | return bus.l * bus.b * bus.h 15 | } 16 | // 重复上面的二三步 17 | type PublicTransporter interface { 18 | PassengerCapacity() int 19 | } 20 | func (bus Bus) PassengerCapacity() int { 21 | return bus.rows * bus.seatsPerRow 22 | } 23 | func main() { 24 | bus := Bus{ 25 | l:10, b:6, h:3, 26 | rows: 10, seatsPerRow: 5} 27 | fmt.Println("Cubic volume of bus: ", bus.CubicVolume()) 28 | fmt.Println("Maximum number of passengers:", bus.PassengerCapacity()) 29 | } 30 | -------------------------------------------------------------------------------- /interface5.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | // Go第一步:定义你的数据结构 4 | type Bus struct { 5 | l, b, h int 6 | rows, seatsPerRow int 7 | } 8 | // Go第二步:定义一个现实世界对象的抽象层 9 | type Cuboider interface { 10 | CubicVolume() int 11 | } 12 | // Go第三步:实现接口的方法 13 | func (bus Bus) CubicVolume() int { 14 | return bus.l * bus.b * bus.h 15 | } 16 | // 重复上面的二三步 17 | type PublicTransporter interface { 18 | PassengerCapacity() int 19 | } 20 | func (bus Bus) PassengerCapacity() int { 21 | return bus.rows * bus.seatsPerRow 22 | } 23 | // 新的项目需求 24 | type PersonalSpaceLaw interface { 25 | IsCampliantWithLaw() bool 26 | } 27 | func (bus Bus) IsCampliantWithLaw() bool { 28 | return ( bus.l * bus.b * bus.h ) / (bus.rows * bus.seatsPerRow) >= 3 29 | } 30 | func main() { 31 | bus := Bus{ 32 | l:10, b:6, h:3, 33 | rows: 10, seatsPerRow: 5} 34 | fmt.Println("Cubic volume of bus: ", bus.CubicVolume()) 35 | fmt.Println("Maximum number of passengers:", bus.PassengerCapacity()) 36 | fmt.Println("Is campliant with law:", bus.IsCampliantWithLaw()) 37 | } 38 | -------------------------------------------------------------------------------- /multipleinheritance.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | type Camera struct {} 4 | func (_ Camera) takePicture() string { 5 | return "Clicked" 6 | } 7 | type Phone struct {} 8 | func (_ Phone) call() string { 9 | return "Ring Ring" 10 | } 11 | type CameraPhone struct { 12 | Camera 13 | Phone 14 | } 15 | func main() { 16 | cp := new(CameraPhone) 17 | fmt.Println("A CameraPhone can take a picture: ", cp.takePicture()) 18 | fmt.Println("A CameraPhone also can make a call: ", cp.call()) 19 | } 20 | -------------------------------------------------------------------------------- /polymorphism1.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | // 定义人这个接口 4 | type Human interface { 5 | // 它有一个方法 iLove 6 | iLove() string 7 | } 8 | // 定义男人 9 | type Man struct {} 10 | // 实现人这个接口 11 | func (man Man) iLove() string { 12 | return "钓鱼" 13 | } 14 | //重复上面的定义 15 | type Woman struct {} 16 | func (woman Woman) iLove() string { 17 | return "购物" 18 | } 19 | func main() { 20 | m := new (Man) 21 | w := new (Woman) 22 | // 一个保存人的数组,它有两人个元素 23 | // 一个为 Man 类型,一个为Woman类型 24 | humans := [...]Human{m,w} 25 | for i, _ := range (humans) { 26 | // 直接调用接口的 iLove 方法 27 | fmt.Println("我喜欢", humans[i].iLove()) 28 | } 29 | } 30 | 31 | -------------------------------------------------------------------------------- /polymorphism2.go: -------------------------------------------------------------------------------- 1 | package main 2 | import "fmt" 3 | // 定义生物这个接口 4 | type Organism interface { 5 | // 它有一个方法 iLove 6 | iLove() string 7 | } 8 | // 定义人这种生物 9 | type Human struct {} 10 | // 定义人对生物这个接口的实现 11 | func (human Human) iLove() string { 12 | return "做人该做的事情,我是一只人,不是一个狗!" 13 | } 14 | // 定义男人 15 | type Man struct { Human } 16 | func (man Man) iLove() string { 17 | return "钓鱼,我是一只男人。" 18 | } 19 | //重复上面的定义 20 | type Woman struct { Human } 21 | func (woman Woman) iLove() string { 22 | return "购物,我是一只女人。" 23 | } 24 | // 定义狗 25 | type Dog struct {} 26 | func (dog Dog) iLove() string { 27 | return "汪!汪汪!汪汪汪!我是一个狗!不是一只人。" 28 | } 29 | func main() { 30 | h := new (Human) 31 | m := new (Man) 32 | w := new (Woman) 33 | d := new (Dog) 34 | organisms := [...]Organism{h, m, w, d} 35 | for i, _ := range (organisms) { 36 | // 直接调用接口的 iLove 方法 37 | fmt.Println("我喜欢", organisms[i].iLove()) 38 | } 39 | } 40 | 41 | -------------------------------------------------------------------------------- /shortit.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "net/http" 6 | "text/template" 7 | "code.google.com/p/google-api-go-client/urlshortener/v1" // 导入GOOGLE API包 8 | ) 9 | 10 | // 本项目的网页模板 11 | var tmpl = template.Must(template.New("URL Shortener Template").Parse(` 12 | 13 | 14 |
15 |