![b683eca350b65b052021fbef4e3c7a60.png](https://img-blog.csdnimg.cn/img_convert/b683eca350b65b052021fbef4e3c7a60.png)
在demo1中,我们简单使用了net/http搭建了一个server,其实在日常开发中,比较少去使用标准库去直接写api,更多的是使用前人搭建好的轮子(我呢,是个不太喜欢重复造轮子的开发者,有开源的靠谱的,直接用就好,自己调整成自己需要的即可),那么说的go的框架,不得不说gin了。
对于gin的介绍,是github上star最好的go框架了,其他不多说,我们上手写起来吧!
目标:
- 自定义配置
- 整合mysql和redis
- 独立路由管理
- 日志
- 平滑重启
- 脚本打包
使用到的库:
- github.com/fsnotify/fsnotify
- github.com/gin-gonic/gin
- github.com/go-redis/redis
- github.com/jinzhu/gorm
- http://github.com/lestrrat-go/file-rotatelogs
- http://go.uber.org/zap
- github.com/spf13/pflag
- github.com/spf13/viper
初始化
modules的引入之后,我们就可以不必使用gopath去管理项目目录了,对于modules的基本使用,建议看文章:
gomodulesgithub.13sai.com![48467a6f12274d724775ffbaec04709f.png](https://img-blog.csdnimg.cn/img_convert/48467a6f12274d724775ffbaec04709f.png)
我们开始:
go mod init sai0556/demo2-gin-frame
因为我们暂时本地开发:
// 使用本地module
go mod edit -require=local.com/sai0556/demo2-gin-frame@v1.0.0
go mod edit -replace=local.com/sai0556/demo2-gin-frame@v1.0.0=$PWD
可以看到go.mod已生成:
module sai0556/demo2-gin-frame
go 1.13
require local.com/sai0556/demo2-gin-frame v1.0.0
replace