Golang Gin Framework Learning record

Source: Internet
Author: User

21-day Boutique blockchain free learning, in-depth expert led the way, to help developers easily play the blockchain! >>>

Reference Documentation:

    1. Golang Micro-Frame Gin Introduction https://www.jianshu.com/p/a31e4ee25305

First, the use of Govendor

Use a vendor tool like Govendorgo get Govendor (install)

$ go get github.com/kardianos/govendor

Create your project folder and CD inside (creating a local project directory and cutting to that directory)

$ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"

Vendor init your project and add gin (generate Vendor file and Vendor.json, and download gin)

$ govendor init$ govendor fetch github.com/gin-gonic/gin@v1.2

Copy a starting template inside your project (copy Https://raw.githubusercontent.com/gin-gonic/gin/master/examples/basic /main.go file to local, measured local main.go is empty, manually copy from $gopath/src/github.com/gin-gonic/gin/examples/basic directory)

$ curl https://raw.githubusercontent.com/gin-gonic/gin/master/examples/basic/main.go > main.go

Run your project

$ go run main.go

Two, Path parameter (Parameters in path) test

When the parameter is in the URL? Before the time, by the value: = C.param ("key") way to get the parameters, when the parameters in the URL? Later, use the value: = C.qury ("Key") method. If swapped, a null character is returned. The source code is as follows:

package mainimport ("github.com/gin-gonic/gin""net/http")func main() {router := gin.Default();router.GET("/welcome/:user/*action", func(c *gin.Context){user := c.Param("user")action := c.Param("action")name := c.DefaultQuery("name", "uname")password := c.Query("password")age := c.Query("age")c.String(http.StatusOK, "user=%s, action=%s, name=%s, password=%s, age=%s\n", user, action, name, password, age)})router.Run()}

The test method is as follows:

$ curl -X GET http://127.0.0.1:8080/welcome/Guest/findsomething?dname\=jerry\&password\=123\&age\=90user=Guest, action=/findsomething, name=uname, password=123, age=90
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.