Giter Club home page Giter Club logo

golang-web's Introduction

GO~

路由

创建路由实例

    route := odserver.Default(

路由方式一:

创建基本路由

route.Get("/user",SayHello)

支持链式

route.Get("/user",SayHello).Get("/user1",SayHello)

统一开始域

route.Start("/new").Get("/1", SayHello).Get("/2", SayHello)

路由方式二

创建基本路由

route.Target("/user").AddGet(SayHello).AddPost(SayHello2)

对路径/user同时注册了Get和Post两种请求方式

统一开始域

route.Start("/cookie").
    Target("/read").GoGet(ReadCookieServer).GoPost(ReadCookieServer).And().
    Target("/write").GoGet(WriteCookieServer)

等价于

route.Target("/cookie/read").GoGet(ReadCookieServer)
route.Target("/cookie/read").GoGet(ReadCookieServer)
route.Target("/cookie/write").GoPost(WriteCookieServer)

参数获取

URL请求参数

/query?name=ljz&age=100

func GetQuery(c *odserver.Context) {
	//c.GoReq().ParseForm()
	getQuery := ""
	for k, v := range c.GoReq().URL.Query() {
		getQuery = getQuery + k + ":" + strings.Join(v, "") + " "
	}
	c.GoResW().Write([]byte(getQuery))
}

body请求参数

route.Target("/params/{id}/{name}").Get(GetParams)

当以/params/110/ljz访问时

func GetParams(c *odserver.Context) {
	fmt.Println(c.Params) // ["110", "ljz"]
	fmt.Println(c.GetParams) // map[id: "110", name: "ljz"]
}

post参数

// 可获取formData和application/json形式的数据提交
c.PostParams()

golang-web's People

Contributors

lanjz avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.