Six implementations of HTTPServer development in Go language

After learning the client implementation of the HTTP protocol interfaces net/http and fasthttp, we will start the development of Server. If we don't learn, we will be surprised. Unexpectedly, these two libraries also support the development of Server. It's too convenient.

Compared with Java, HTTP Server development basically uses Spring or Springboot framework, and always configures various configuration classes and handle objects. The Server development of Golang is very simple because it is very simple, or there is no special unified specification or framework. I have found many implementation methods. The HTTP protocol is still based on net/http and fasthttp, but the handle syntax is diverse.

First review: Practice of HTTP client in Golang language,Golang fasthttp practice.

In terms of Golang language, there may be many libraries to realize a certain function. If you have the opportunity, you should communicate with your peers. You may find a better library to use. Next, I share the implementation demos of six kinds of Server development I learned.

First kind

Based on net/http implementation, this is a relatively basic method. It is not elegant to deal with the mapping relationship between interface and handle, 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 net/http Created server The first way")
   fmt.Fprintf(w, task.FunTester)
 log.Println("Start creating HTTP service")



The second is also based on net/http. This writing syntax can well solve the problem of the first. handle and path have similar configured syntax, and their readability is greatly improved.

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 The second is to create service syntax"})
 http.Handle("/", &indexHandler{content: task.FunTester})
 http.ListenAndServe(":8001", nil)


The third is based on net/http and The latter mainly provides Echo objects to handle various configurations, including interface and handle mapping. It has rich functions and the best readability.

func TestHttpSer3(t *testing.T) {
 app := echo.New()
  AllowOrigins: []string{"*"},
  AllowMethods: []string{echo.GET, echo.DELETE, echo.POST, echo.OPTIONS, echo.PUT, echo.HEAD},
  AllowHeaders: []string{echo.HeaderContentType, echo.HeaderAuthorization},
  projectGroup := app.Group("/test")
  projectGroup.GET("/", PropertyAddHandler)
 app.Server.Addr = ":8001"



The fourth is still based on net/http, and introduces the route of It seems that the mapping relationship between interface and handle 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) {
    "code": 0,
    "msg":  "This is creating HTTPServer The fourth way",

 s := &http.Server{
  Addr:           ":8001",
  Handler:        router,
  ReadTimeout:    1000 * time.Second,
  WriteTimeout:   1000 * time.Second,
  MaxHeaderBytes: 1 << 20,


The fifth is developed based on fasthttp, which uses the API s provided by fasthttp. It is readable, and the handle configuration is more like Java.

func TestFastSer(t *testing.T) {
 address := ":8001"
 handler := func(ctx *fasthttp.RequestCtx) {
  path := string(ctx.Path())
  switch path {
  case "/test":
   ctx.SetBody([]byte("This is fasthttp The first syntax for creating a service"))
 s := &fasthttp.Server{
  Handler: handler,
  Name:    "FunTester server",

 if err := s.ListenAndServe(address); err != nil {
  log.Fatal("error in ListenAndServe", err.Error())



The sixth one is still based on fasthttp and uses It's a little strange that the two are not in a GitHub warehouse. The use of grammar is a little similar to the third way. It is more organized and conducive to reading.

func TestFastSer2(t *testing.T) {
 address := ":8001"

 router := fasthttprouter.New()
 router.GET("/test", func(ctx *fasthttp.RequestCtx) {
  ctx.Response.SetBody([]byte("This is fasthttp establish server The second grammar of"))
 router.GET("/", func(ctx *fasthttp.RequestCtx) {
 fasthttp.ListenAndServe(address, router.Handler)

- END -

Posted on Thu, 04 Nov 2021 04:46:29 -0400 by allyse