Skip to content

Commit 205a7e5

Browse files
committed
Update:新增文章
1 parent 5dc4e5e commit 205a7e5

File tree

12 files changed

+524
-6
lines changed

12 files changed

+524
-6
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
- 1.11 [流程控制:goto 无条件跳转](http://golang.iswbm.com/en/latest/c01/c01_11.html)
1515
- 1.12 [流程控制:defer 延迟语句](http://golang.iswbm.com/en/latest/c01/c01_12.html)
1616
- 1.13 [异常机制:panic 和 recover](http://golang.iswbm.com/en/latest/c01/c01_13.html)
17+
- 1.14 [Type Assertion 与 Type Switch](http://golang.iswbm.com/en/latest/c01/c01_14.html)
18+
- 1.15 [信道里使用 select](http://golang.iswbm.com/en/latest/c01/c01_15.html)
1719

1820
## 第二章:面向对象
1921
- 2.1 [面向对象编程:结构体与继承](http://golang.iswbm.com/en/latest/c02/c02_01.html)
@@ -38,8 +40,11 @@
3840
- 4.8 [学习一些常见的并发模型](http://golang.iswbm.com/en/latest/c04/c04_08.html)
3941
- 4.9 [函数式编程](http://golang.iswbm.com/en/latest/c04/c04_09.html)
4042

41-
## 第五章:暂未分类
42-
- 5.1 [推荐几个Go学习网站](http://golang.iswbm.com/en/latest/c05/c01_01.html)
43+
## 第五章:Web实战
44+
- 6.1 [Gin 实战:Hello World](http://golang.iswbm.com/en/latest/c05/c05_01.html)
45+
46+
## 第六章:暂未分类
47+
- 6.1 [推荐几个Go学习网站](http://golang.iswbm.com/en/latest/c06/c06_01.html)
4348

4449

4550
---

source/c01/c01_14.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 1.14 Type Assertion 与 Type Switch
2+
3+
4+
5+
https://sanyuesha.com/2017/12/01/go-interface-4/
6+
7+
https://maiyang.me/post/2018-08-30-type-assertion-switch/
8+
9+
https://www.kancloud.cn/hartnett/gopl-zh/126063
10+
11+
https://segmentfault.com/a/1190000016230264

source/c01/c01_14.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
1.14 Type Assertion 与 Type Switch
2+
==================================
3+
4+
https://sanyuesha.com/2017/12/01/go-interface-4/
5+
6+
https://maiyang.me/post/2018-08-30-type-assertion-switch/
7+
8+
https://www.kancloud.cn/hartnett/gopl-zh/126063
9+
10+
https://segmentfault.com/a/1190000016230264

source/c01/c01_15.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 1.15 信道里使用 select
2+
3+
4+
5+
待完成

source/c01/c01_15.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1.15 信道里使用 select
2+
======================
3+
4+
待完成

source/c05/c05_01.md

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
# 6.1 Gin 实战:Hello World
2+
3+
## 1. 最简单的web示例
4+
5+
新建一个项目目录,比如我使用 Goland ,那我的项目目录就为 `~/GolandProjects/gin-web-example`
6+
7+
然后使用 go mod 命令初始化
8+
9+
```shell
10+
$ go mod init github.com/iswbm/gin-web-example
11+
go: creating new go.mod: module github.com/iswbm/gin-web-example
12+
```
13+
14+
按照之前讲过的,此时会生成 `go.mod` 文件。
15+
16+
接着,还是在这个目录下,新建一个 `main.go` 的文件
17+
18+
```go
19+
package main
20+
21+
import (
22+
"github.com/gin-gonic/gin"
23+
)
24+
25+
26+
func main(){
27+
r :=gin.Default()
28+
r.GET("/ping", func(c *gin.Context) {
29+
c.JSON(200, gin.H{
30+
"message": "pong",
31+
})
32+
})
33+
34+
// 默认在监听 0.0.0.0:8080
35+
r.Run()
36+
}
37+
```
38+
39+
一切准备好后,就可以运行了,
40+
41+
```
42+
$ go run main.go
43+
```
44+
45+
从输出来看,它提示我们监听端口是 8080
46+
47+
![](/Users/MING/Library/Application Support/typora-user-images/image-20200324211928616.png)
48+
49+
我们可以使用浏览器试着访问一下 `0.0.0.0:8080/ping`,如下图所示,访问成功。
50+
51+
![](/Users/MING/Library/Application Support/typora-user-images/image-20200324205302201.png)
52+
53+
54+
55+
## 2. 项目结构及依赖管理
56+
57+
以之前创建的项目目录为根目录,继续创建如下目录
58+
59+
- conf:用于存储配置文件
60+
- middleware:应用中间件
61+
- models:应用数据库模型
62+
- pkg:第三方包
63+
- routers 路由逻辑处理
64+
- runtime:应用运行时数据
65+
66+
最终效果如下
67+
68+
```
69+
$ tree
70+
.
71+
├── conf
72+
├── go.mod
73+
├── go.sum
74+
├── main.go
75+
├── middleware
76+
├── models
77+
├── pkg
78+
├── routes
79+
└── runtime
80+
```
81+
82+
在这个目录下,每个文件夹下都是一个模块。
83+
84+
由于最开始我们定义了模块名为 `github.com/iswbm/gin-web-example` ,所以当我们在一个模块中引用另一个模块的内容,也必须会从这里`github.com/iswbm/gin-web-example` 导入,但是当我们还在开发阶段时,这段模块还未上传至 github 仓库,导入时肯定会失败。
85+
86+
也就是说,你的每次调试都必须先将你的代码全部上传至 github ,项目才能运行。
87+
88+
那有什么办法可以解决这种低效的开发模式呢?
89+
90+
这时候,就要提到之前提过的 go.mod 里的 replace 关键字。
91+
92+
比如 `github.com/iswbm/gin-web-example/routers => ./routers` 这句话后,当我们在项目中导入 `github.com/iswbm/gin-web-example/routers` 时,它不会去 github 上查找拉取包,而是会从本地的 `./routes` 查找。
93+
94+
```
95+
module github.com/iswbm/gin-web-example
96+
97+
go 1.14
98+
99+
require github.com/gin-gonic/gin v1.6.1
100+
101+
replace (
102+
github.com/iswbm/gin-web-example/conf => ./pkg/conf
103+
github.com/iswbm/gin-web-example/middleware => ./middleware
104+
github.com/iswbm/gin-web-example/models => ./models
105+
github.com/iswbm/gin-web-example/pkg/setting => ./pkg/setting
106+
github.com/iswbm/gin-web-example/routers => ./routers
107+
)
108+
```
109+
110+
111+
112+
## 3. 数据库创建
113+
114+
115+
116+
![image-20200325203348548](/Users/MING/Library/Application Support/typora-user-images/image-20200325203348548.png)
117+
118+
119+
120+
使用这段 SQL 命令,创建标签表
121+
122+
```sql
123+
CREATE TABLE `blog_tag` (
124+
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
125+
`name` varchar(100) DEFAULT '' COMMENT '标签名称',
126+
`created_on` int(10) unsigned DEFAULT '0' COMMENT '创建时间',
127+
`created_by` varchar(100) DEFAULT '' COMMENT '创建人',
128+
`modified_on` int(10) unsigned DEFAULT '0' COMMENT '修改时间',
129+
`modified_by` varchar(100) DEFAULT '' COMMENT '修改人',
130+
`deleted_on` int(10) unsigned DEFAULT '0',
131+
`state` tinyint(3) unsigned DEFAULT '1' COMMENT '状态 0为禁用、1为启用',
132+
PRIMARY KEY (`id`)
133+
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='文章标签管理';
134+
```
135+
136+
137+
138+
![image-20200325203447004](/Users/MING/Library/Application Support/typora-user-images/image-20200325203447004.png)
139+
140+
使用这段 SQL 命令,创建文章表
141+
142+
```sql
143+
CREATE TABLE `blog_article` (
144+
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
145+
`tag_id` int(10) unsigned DEFAULT '0' COMMENT '标签ID',
146+
`title` varchar(100) DEFAULT '' COMMENT '文章标题',
147+
`desc` varchar(255) DEFAULT '' COMMENT '简述',
148+
`content` text,
149+
`created_on` int(11) DEFAULT NULL,
150+
`created_by` varchar(100) DEFAULT '' COMMENT '创建人',
151+
`modified_on` int(10) unsigned DEFAULT '0' COMMENT '修改时间',
152+
`modified_by` varchar(255) DEFAULT '' COMMENT '修改人',
153+
`deleted_on` int(10) unsigned DEFAULT '0',
154+
`state` tinyint(3) unsigned DEFAULT '1' COMMENT '状态 0为禁用1为启用',
155+
PRIMARY KEY (`id`)
156+
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='文章管理';
157+
3、 认证表
158+
```
159+
160+
使用这段 SQL 命令,创建认证表
161+
162+
```sql
163+
CREATE TABLE `blog_auth` (
164+
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
165+
`username` varchar(50) DEFAULT '' COMMENT '账号',
166+
`password` varchar(50) DEFAULT '' COMMENT '密码',
167+
PRIMARY KEY (`id`)
168+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
169+
170+
INSERT INTO `blog`.`blog_auth` (`id`, `username`, `password`) VALUES (null, 'test', 'test123456');
171+
```
172+
173+
174+
175+
## 4. 配置文件
176+
177+
在 conf 目录下新建 app.ini 文件,内容如下
178+
179+
```ini
180+
#debug or release
181+
RUN_MODE = debug
182+
183+
[app]
184+
PAGE_SIZE = 10
185+
JWT_SECRET = 23347$040412
186+
187+
[server]
188+
HTTP_PORT = 8000
189+
READ_TIMEOUT = 60
190+
WRITE_TIMEOUT = 60
191+
192+
[database]
193+
TYPE = mysql
194+
USER = 数据库账号
195+
PASSWORD = 数据库密码
196+
#127.0.0.1:3306
197+
HOST = 数据库IP:数据库端口号
198+
NAME = blog
199+
TABLE_PREFIX = blog_
200+
```
201+
202+
203+
204+
205+

0 commit comments

Comments
 (0)