Go Server----Use gin framework to build a simple service

Source: Internet
Author: User
Tags app service
This is a creation in Article, where the information may have evolved or changed.

Use the gin framework to build a simple service

Go Language web Framework is very many, each has its own characteristics and style. The reason why I use the gin framework in my project is because the project started out as a martini, a well-designed framework, but there is a serious problem with too much use of reflection to make it too inefficient (the problem also caused the program to rise too fast in the event of traffic spikes), And this framework in the last year no one to maintain, and the author recommended the use of a very similar style of gin framework, probably looked at, the style is really similar, and high efficiency, so the use of gin. As for the other frameworks, it is beego that the project also uses its subproject beelog, but because the project I am doing is biased towards the application service rather than the website service, its style is more suitable for the website service, so there is no choice for it.

With these two frames on the GitHub warehouse, Martini doesn't recommend
Gin
Beego

Here's a quick code snippet for building a simple app service with gin, and it's easy to see the code comments.

Package Mainimport ("FMT"    "Github.com/gin-gonic/gin"    "Net/http") Func main () {gin. SetMode(Gin. DebugMode)//Global Settings environment, this is the development environment, the online environment is gin. ReleasemodeRouter: = Gin. Default()//Get route instance//Add middleware router. use(middleware)//Register interface router. GET("/simple/server/get", GetHandler) router. POST("/simple/server/post", Posthandler) router. PUT("/simple/server/put", Puthandler) router. DELETE("/simple/server/delete", Deletehandler)//Listening port HTTP. Listenandserve(": 8005", router)}func middleware (c *gin. Context) {FMT. Println("This is a middleware!")}func GetHandler (c *gin. Context) {value, exist: = C. Getquery("Key") if!exist {value ="The key is not exist!"} C. Data(http. Statusok,"Text/plain", []byte (FMT. Sprintf("Get success! %s\n ", value))) Return}func Posthandler (c *gin. Context{type jsonholder struct {Id int ' JSON:"id"' Name string ' JSON:"Name"'} Holder: = Jsonholder{id:1, Name:"My Name"}//If the JSON data is returned, you can use the Gin encapsulated JSON method C directly. JSON(http. Statusok, holder) Return}func Puthandler (c *gin. Context) {C. Data(http. Statusok,"Text/plain", []byte ("Put success!\n")) Return}func Deletehandler (c *gin. Context) {C. Data(http. Statusok,"Text/plain", []byte ("Delete success!\n")) return}

Access these interfaces with the Curl command to get the corresponding return result

Log output of the program

Here is just a quick way to get started, as a Web services framework, the function is certainly very powerful, which needs to be in the actual use of the slowly digging and drill down.

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.