Skip to content

Commit 1f3b6a5

Browse files
CoxCox
Cox
authored and
Cox
committed
first commit
0 parents  commit 1f3b6a5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1063
-0
lines changed

.struct2.go.swp

12 KB
Binary file not shown.

README.md

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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)

anonymousfields1.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

anonymousfields2.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
}

anonymousfields3.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
}

channels1.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
}

channels2.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

channels3.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}

channels4.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
}

channels5.go

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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+
}

forloop1.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}

forloop2.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

forloop3.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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+
}

forloop4.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}

forloop5.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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+
}

forloop6.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

forloop7.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
}

goerror1.go

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import "fmt"
4+
import "os"
5+
6+
func main() {
7+
fmt.Println("Hello World!")
8+
}

goerror2.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.println("Hello World!")
7+
}

goerror3.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("Hello"); fmt.Println("World!");
7+
}

goerror4.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "fmt";;
4+
5+
func main() {
6+
fmt.Print("Hello World!")
7+
}

0 commit comments

Comments
 (0)