Get the App
SLTechnology News&Howtos  ›  Development  › 

There are several ways to develop Go language HTTPServer.

Shulou Source: shulou.com Published: 2022-06-02 18:42:14 10月04日 Update

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

Tags: Way grammar this is development language learning readability interface service configuration more problem processing help practical elegant next three two warehouse Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Shulou Tech Info Xiaomi MySQL Shulou Information