gow 是一个基于gin框架思想和beego框架中html模板处理机制,封装的一个go web框架。可用于开发Web API和Web网站项目。
mkdir hello
cd hello
go mod init
go get github.com/gkzy/gow
package main
import (
"github.com/gkzy/gow"
)
func main() {
r := gow.Default()
r.GET("/", func(c *gow.Context) {
c.JSON(gow.H{
"code": 0,
"msg": "success",
})
})
r.Run()
}
go build && ./hello
浏览器访问:http://127.0.0.1:8080
curl -i http://127.0.0.1:8080
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Wed, 15 Jul 2020 09:15:31 GMT
Content-Length: 27
{"code":0,"msg":"success"}
包括html模板文件处理和静态资源处理
PROJECT_NAME
├──static
├── img
├──111.jpg
├──222.jpg
├──333.jpg
├──js
├──css
├──views
├──index.html
├──article
├──detail.html
├──main.go
r.SetView("views")
r.Static("/static", "static")
main.go
package main
import (
"github.com/gkzy/gow"
)
func main() {
r := gow.Default()
r.AutoRender = true //打开html模板渲染
r.SetView("views") //默认静态目录为views时,可不调用此方法
r.StaticFile("favicon.ico","static/img/log.png") //路由favicon.ico
r.Static("/static", "static")
//router
r.Any("/", IndexHandler)
r.Any("/article/1", ArticleDetailHandler)
//自定义hello的模板函数
//在模板文件可通过 {{hello .string}} 来执行
r.AddFuncMap("hello", func(str string) string {
return "hello:" + str
})
r.Run()
}
//IndexHandler 首页handler
func IndexHandler(c *gow.Context) {
c.HTML("index.html", gow.H{
"name": "gow",
"package": "github.com/gkzy/gow",
})
}
//ArticleDetailHandler 文章详情页handler
func ArticleDetailHandler (c *gow.Context){
c.HTML("article/detail.html", gow.H{
"title": "年薪百万的文科专业有哪些?",
})
}
views/index.html
<html>
<head>
<title>{{hello .name}}</title>
<meta charset="utf-8"/>
</head>
<body>
<h2>{{.name}}</h2>
<hr>
<h5>{{.package}}</h5>
</body>
</html>
views/article/detail.html
<html>
<head>
<title>{{.title}}</title>
<meta charset="utf-8"/>
<style>
img{max-width:600px;}
</style>
</head>
<body>
<h2>{{.title}}</h2>
<hr>
<p><img src="/static/img/111.jpg"></p>
<p><img src="/static/img/222.jpg"></p>
<p><img src="/static/img/333.jpg"></p>
</body>
</html>
go run main.go
或
go build main.go -o app && ./app
https://127.0.0.1:8080/
https://127.0.0.1:8080/article/1
func GetUser(c *gow.Context){
//获取字串
c.GetString("key","default")
//获取int
c.GetInt("key",0)
//获取bool
c.GetBool("key",false)
//获取int64
c.GetInt64("key",-1)
//获取float
c.GetFloat("key",0)
//获取[]string
var ids []string
ids = c.GetStrings("ids")
//其他方法
c.GetInt32()
c.GetInt16()
c.GetInt8()
....
}
获取 request body,并反序列化到 struct
type User struct {
Nickname string `json:"nickname"`
QQ string `json:"qq"`
}
func GetUser(c *Context){
user := new(User)
err := c.DecodeJSONBody(&user)
if err != nil {
//handler error
}
c.JSON(gow.H{
"user": user,
})
}
支持:json xml string html 等方式输出
JSON
func GetUser(c *Context){
//default http.StatusOK
c.JSON(gow.H{
"nickname":"gow",
"age":18,
})
//或者,指定 http.StatusCode
c.ServerJSON(200,gow.H{
"nickname":"gow",
"age":18,
})
}
XML
func GetUser(c *Context){
//default http.StatusOK
c.XML(gow.H{
"nickname":"gow",
"age":18,
})
//或者,指定 http.StatusCode
c.ServerXML(200,gow.H{
"nickname":"gow",
"age":18,
})
}
String
func GetUser(c *Context){
//default http.StatusOK
c.String("hello gow...")
//或者,指定 http.StatusCode
c.ServerString(200,"hello gow...")
}
File
func GetUser(c *Context){
//读取并输出
c.File("go.mod")
}
HTTPMethod = map[string]bool{
"GET": true,
"POST": true,
"PUT": true,
"DELETE": true,
"PATCH": true,
"OPTIONS": true,
"HEAD": true,
"TRACE": true,
}
包括基本路由与分组
r := gow.Default()
r.GET(path,handler)
r.POST(path,handler)
r.PUT(path,handler)
r.DELETE(path,handler)
r.PATCH(path,handler)
r.OPTIONS(path,handler)
r.HEAD(path,handler)
r.TRACE(path,handler)
r.Any("/article/:id", ArticleDetailHandler)
获取 param 值
id:=c.Param("id")
main.go
package main
import (
"github.com/gkzy/gow"
)
func main() {
r := gow.Default()
v1 := r.Group("/v1")
{
v1.GET("/user/:id", GetUser)
v1.DELETE("/user/:id", DeleteUser)
}
r.Run()
}
func GetUser(c *gow.Context) {
c.JSON(gow.H{
"nickname": "新月却泽滨",
"qq": "301109640",
})
}
func DeleteUser(c *gow.Context) {
c.JSON(gow.H{
"code": 0,
"msg": "success",
})
}
Get
curl -i http://127.0.0.1:8080/v1/user/1
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Thu, 16 Jul 2020 05:55:16 GMT
Content-Length: 46
{
"nickname": "新月却泽滨",
"qq": "301109640"
}
Delete
curl -X "DELETE" http://127.0.0.1:8080/v1/user/1
{
"code":0,
"msg":"success"
}