go-restful实战与深入分析之使用篇
来源:互联网 发布:mac官网 编辑:程序博客网 时间:2024/04/20 07:37
go的rest框架很多国内最著名应该是beego,但这个框架设计思想比较老套而且很笨重,如果需要一个轻量级的框架我个人挺喜欢go-restful先看看怎么使用,如果有过Flask或者springMVC的经验,这个很容易上手,所以我个人觉得语言是想通的,先学好一门语言其它的按照套路来就可以了,先看代码例子:
package mainimport ( "log" "net/http" "github.com/emicklei/go-restful")// This example has the same service definition as restful-user-resource// but uses a different router (CurlyRouter) that does not use regular expressions//// POST http://localhost:8080/users// <User><Id>1</Id><Name>Melissa Raspberry</Name></User>//// GET http://localhost:8080/users/1//// PUT http://localhost:8080/users/1// <User><Id>1</Id><Name>Melissa</Name></User>//// DELETE http://localhost:8080/users/1//type User struct { Id, Name string}type UserResource struct { // normally one would use DAO (data access object) users map[string]User}func (u UserResource) Register(container *restful.Container) { ws := new(restful.WebService) ws. Path("/users"). Consumes(restful.MIME_XML, restful.MIME_JSON). Produces(restful.MIME_JSON, restful.MIME_XML) // you can specify this per route as well ws.Route(ws.GET("/{user-id}").To(u.findUser)) ws.Route(ws.POST("").To(u.updateUser)) ws.Route(ws.PUT("/{user-id}").To(u.createUser)) ws.Route(ws.DELETE("/{user-id}").To(u.removeUser)) container.Add(ws)}// GET http://localhost:8080/users/1//func (u UserResource) findUser(request *restful.Request, response *restful.Response) { id := request.PathParameter("user-id") usr , ok := u.users[id] if !ok { response.AddHeader("Content-Type", "text/plain") response.WriteErrorString(http.StatusNotFound, "User could not be found.") } else { response.WriteEntity(usr) }}// POST http://localhost:8080/users// <User><Id>1</Id><Name>Melissa Raspberry</Name></User>//func (u *UserResource) updateUser(request *restful.Request, response *restful.Response) { usr := new(User) err := request.ReadEntity(&usr) if err == nil { u.users[usr.Id] = *usr response.WriteEntity(usr) } else { response.AddHeader("Content-Type", "text/plain") response.WriteErrorString(http.StatusInternalServerError, err.Error()) }}// PUT http://localhost:8080/users/1// <User><Id>1</Id><Name>Melissa</Name></User>//func (u *UserResource) createUser(request *restful.Request, response *restful.Response) { usr := User{Id: request.PathParameter("user-id")} err := request.ReadEntity(&usr) if err == nil { u.users[usr.Id] = usr response.WriteHeaderAndEntity(http.StatusCreated, usr) } else { response.AddHeader("Content-Type", "text/plain") response.WriteErrorString(http.StatusInternalServerError, err.Error()) }}// DELETE http://localhost:8080/users/1//func (u *UserResource) removeUser(request *restful.Request, response *restful.Response) { id := request.PathParameter("user-id") delete(u.users, id)}func main() { wsContainer := restful.NewContainer() wsContainer.Router(restful.CurlyRouter{}) u := UserResource{map[string]User{}} u.Register(wsContainer) log.Printf("start listening on localhost:8080") server := &http.Server{Addr: ":8080", Handler: wsContainer} log.Fatal(server.ListenAndServe())}
代码很简单,users的一套CURD。这里面有几个点说一下,首先需要定义一个web container的容器wsContainer,然后把webservice放到容器里面,可以理解成一个container里面有很多的webservice,每个webservice里面有先定义Path路径,在这里面是/users,在这个路径下面会有CURD的方法,通过Route去确定方法匹配和跳转。启动上面代码就可以测试了,先测试一个创建:
POST127.0.0.1:8080/users{"Id":"43a","Name":"eee"}
再测试一个查询
GET127.0.0.1:8080/users/43a
当然我不想止步于此,具体看看这个go-restful是怎样实现的。
0 0
- go-restful实战与深入分析之使用篇
- go-restful实战与深入分析之基础篇
- go-restful实战与深入分析之源码篇
- Go语言RESTful API开发实战
- go-restful之hello world
- 使用Go构建RESTful的JSON API
- Go实战--go语言中使用YAML配置文件(与json、xml、ini对比)
- Go实战--go语言中使用YAML配置文件(与json、xml、ini对比)
- 【原创】k8s源码分析------第三方库go-restful分析
- 深入分析类与对象--综合实战:简单java类
- Go实战--go中使用libphonenumber(The way to go)
- Go实战--go中使用cookie(The way to go)
- Go实战--go中使用rpc(The way to go)
- Go实战--golang中使用go-spew(davecgh/go-spew)
- Go实战--实现简单的restful api(The way to go)
- MyCat之原理简易分析与实战
- Hadoop实战-中高级部分 之 Hadoop RestFul
- CXF实战之RESTFul服务(七)
- 解决IEM被禁用的问提
- Altium Designer原理图设计
- 常用的键盘值
- 左滑tableview出现删除按钮
- 数据结构——特殊矩阵的压缩存储
- go-restful实战与深入分析之使用篇
- CentOS cp 复制隐藏文件提示 cp: cannot stat ?.xxx*?. No such file or directory
- POJ 1947 Rebuilding Roads (树形dp)
- Seven Segment Display----数位dp
- NOI金牌班 导学
- 关联规则FpGrowth算法
- caffe的layers转layer
- unrecognized selector sent to instance出现的原因和解决方案
- Java并发编程实战