In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "there are several ways of Go language HTTPServer development". In daily operation, I believe that many people have doubts about the ways of Goe language HTTPServer development. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the doubts of "what are the ways of Go language HTTPServer development?" Next, please follow the editor to study!
First kind
Based on net/http implementation, this is relatively basic and is not elegant for interface and handle mapping, so it is not recommended.
Func TestHttpSer (t * testing.T) {server: = http.Server {Addr: ": 8001", Handler: http.HandlerFunc (func (w http.ResponseWriter, r * http.Request) {if strings.Index (r.URL.String (), "test") > 0 {fmt.Fprintf (w, "this is the first way of server created by net/http") return} fmt.Fprintf (w, task.FunTester) return}) } server.ListenAndServe () log.Println ("start creating HTTP service")} second kind
The second is also based on net/http, which can solve the first problem very well. Handle and path have similar configuration syntax, which makes it much more readable.
Type indexHandler struct {content string} func (ih * indexHandler) ServeHTTP (w http.ResponseWriter, r * http.Request) {fmt.Fprintf (w, ih.content)} func TestHttpSer2 (t * testing.T) {http.Handle ("/ test", & indexHandler {content: "this is net/http 's second syntax for creating services"}) http.Handle ("/", & indexHandler {content: task.FunTester}) http.ListenAndServe (": 8001", nil)} third
The third is based on net/http and github.com/labstack/echo, which mainly provides Echo objects to handle various configurations, including interfaces and handle mappings, with rich features and the best readability.
Func TestHttpSer3 (t * testing.T) {app: = echo.New () app.Use (middleware.CORSWithConfig (middleware.CORSConfig {AllowOrigins: [] string {"*"}, AllowMethods: [] string {echo.GET, echo.DELETE, echo.POST, echo.OPTIONS, echo.PUT, echo.HEAD}, AllowHeaders: [] string {echo.HeaderContentType, echo.HeaderAuthorization}) }) app.Group ("/ test") {projectGroup: = app.Group ("/ test") projectGroup.GET ("/", PropertyAddHandler)} app.Server.Addr = ": 8001" gracehttp.Serve (app.Server)} fourth
The fourth is still based on net/http implementation, introducing github.com/gin-gonic/gin routing, it seems that the interface and handle mapping relationship is relatively clear.
Func TestHttpServer4 (t * testing.T) {router: = gin.New () api: = router.Group ("/ okreplay/api") {api.POST ("/ submit", gin.HandlerFunc (func (context * gin.Context) {context.ShouldBindJSON (map [string] interface {} {"code": 0, "msg": "this is the fourth way to create HTTPServer" }) context.Status})} s: = & http.Server {Addr: ": 8001", Handler: router, ReadTimeout: 1000 * time.Second, WriteTimeout: 1000 * time.Second, MaxHeaderBytes: 1
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.