-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
172 lines (142 loc) · 3.26 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
// 设置一下输出格式
log.SetFlags(log.Lshortfile | log.Ltime)
log.Println("Hello World!")
/* 面向对象学习 */
//oopTest()
/* Interface 学习*/
interfaceTest()
/* 启动Web服务 */
//requestTest()
}
// 面向对象 oop test
func oopTest() {
普通人 := Human{
name: "普通人",
age: 48,
phone: "13812345678",
}
学生 := Student{
Human: Human{
name: "何同学",
age: 8,
phone: "123001",
},
school: "北京邮电大学",
loan: -1800,
}
打工人 := Employee{
Human: Human{
name: "打工人",
age: 30,
phone: "996996",
},
company: "字节跳动",
money: 30000,
}
普通人.SayHi()
学生.SayHi()
打工人.SayHi()
}
// 注册接口 request test
func requestTest() {
// 启动一个Get请求
listenGetWelcomeRequest()
listenRegister()
// 阻塞端口,监听请求
http.ListenAndServe(":9000", nil)
}
// 接口测试 interface test
type Men interface {
SayHi()
Sing(lyrics string)
}
func interfaceTest() {
mike := Student{Human{"Mike", 25, "111-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{"Tom", 37, "222-444-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)
//这三个都是不同类型的元素,但是他们实现了interface同一个接口
x[0], x[1], x[2] = paul, sam, mike
for _, value := range x {
value.SayHi()
}
// 测试实现String的接口,重写desperation
Bob := Human{"Bob", 39, "000-7777-XXX"}
fmt.Println("This Human is : ", Bob)
}
// 模拟一个不含参的Get请求
func listenGetWelcomeRequest() {
http.HandleFunc("/welcome", func(writer http.ResponseWriter, request *http.Request) {
if request.Method == "GET" {
writer.Write([]byte("Let's go"))
}
})
}
// 声明结构体(类) ``json别名
type User struct {
Name string `json:"username"`
Password string `json:"password"`
}
type Response struct {
Code int
Message string
}
// 注册的内部实现
func register(writer http.ResponseWriter, request *http.Request) {
var res Response
// 延时返回,return之后执行
defer func() {
//将model对象序列化成json对象
arr, _ := json.Marshal(res)
//取第一个参数通过writer返回
writer.Write(arr)
}()
if request.Method != "POST" {
return //无效返回
}
// 解析入参
bds, err := ioutil.ReadAll(request.Body)
if err != nil {
log.Println(err)
return
}
user := User{}
json.Unmarshal(bds, &user)
if len(user.Name) < 4 || len(user.Password) < 6 {
res.Code = 100001
res.Message = "注册参数不合法"
} else {
res.Code = 100000
res.Message = "注册成功"
}
return
}
// 模拟一个注册的POST请求
func listenRegister() {
http.HandleFunc("/register", register)
}
// 模拟一个登陆的POST请求