https://github.com/qlu1990/gos
demo web framework
https://github.com/qlu1990/gos
framework golang-server web
Last synced: about 1 month ago
JSON representation
demo web framework
- Host: GitHub
- URL: https://github.com/qlu1990/gos
- Owner: qlu1990
- License: apache-2.0
- Created: 2019-07-11T13:06:02.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-07-17T14:24:13.000Z (almost 7 years ago)
- Last Synced: 2025-12-17T11:20:27.029Z (4 months ago)
- Topics: framework, golang-server, web
- Language: Go
- Size: 40 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 狗子
- 不错这个webframework 就是叫狗子,我的golang 成长之路
# start
```golang
package main
import "github.com/qlu1990/gos"
func hello(c *gos.Context) {
fmt.Fprintln(c.ResponseWriter, "hello world")
}
func main(){
r := gos.NewGos()
r.AddGet("/hello", hello)
r.Run(":8000") //runing listen port 8000
}
```
# 路由
- ~~路由使用map 可以优化 可以参考gin 使用属树~~(已经实现)
- ~~路由还不能支持路径中带参数~~ (已实现)
## 路由函数
```golang
type HandlerFunc func(*Context)
r := gos.NewGos()
r.AddGet(url string ,f HandlerFunc) // 路由get
r.AddPost(url string ,f HandlerFunc) // 路由post
r.AddHead(url string ,f HandlerFunc) // 路由head
r.AddDelete(url string ,f HandlerFunc) // 路由delete
```
- 请求路径中带参数
```golang
func GetPerson(c *gos.Context) {
name := c.Param("name")
gos.Response(c.ResponseWriter,"hello "+name, http.StatusOK)
}
func main(){
r := gos.NewGos()
r.AddGet("/hello/:name",GetPerson)
}
```
# Middleware
## 实现数据结构
- `Name` 中间件名称不能重名
- `HandlerFunc` 被调用调用函数
```golang
type Middleware struct {
Name string
HandlerFunc HandlerFunc
}
type HandlerFunc func(*Context)
```
## 使用方法
以glog 为例
```golang
r := gos.NewGos()
var Mlog = Middleware{
Name: "log",
HandlerFunc: func(c *Context) {
Glog.Info("Request : ", c.Request.Method, " ", c.Request.RequestURI)
},
}
r.Use(Mlog)
```
#日志模块
## 使用
```golang
r := gos.NewGos()
r.Info("info log")
r.Debug("debug log")
r.Error("error log")
r.Fatal("fatal log")
r.Warn("warn log")
```
# 案例
[案例 gos-example](https://github.com/qlu1990/gos-example)
email: 876392131@qq.com