Six Practical Ways to Build HTTP Servers in Go with net/http, fasthttp, Echo, and Gin
After mastering Go's net/http and fasthttp client libraries, this article presents six distinct server‑side implementations—ranging from basic net/http handlers to Echo, Gin, and fasthttprouter—highlighting their syntax, readability, and configuration differences for rapid backend development.
Having completed the client‑side tutorials for Go's net/http and fasthttp packages, the author explores server development in Go, noting that unlike Java's Spring‑based approach, Go offers concise, framework‑free options as well as lightweight frameworks.
First approach – plain net/http (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, "这是net/http创建的server第一种方式")
return
}
fmt.Fprintf(w, task.FunTester)
return
}),
}
server.ListenAndServe()
log.Println("开始创建HTTP服务")
}Second approach – improved net/http with custom handler struct
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: "这是net/http第二种创建服务语法"})
http.Handle("/", &indexHandler{content: task.FunTester})
http.ListenAndServe(":8001", nil)
}Third approach – net/http combined with the Echo framework
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 approach – net/http with the Gin framework
func TestHttpServer4(t *testing.T) {
router := gin.New()
api := router.Group("/api") {
api.POST("/submit", gin.HandlerFunc(func(context *gin.Context) {
context.ShouldBindJSON(map[string]interface{}{
"code": 0,
"msg": "这是创建HTTPServer第四种方式",
})
context.Status(200)
}))
}
s := &http.Server{
Addr: ":8001",
Handler: router,
ReadTimeout: 1000 * time.Second,
WriteTimeout: 1000 * time.Second,
MaxHeaderBytes: 1 << 20,
}
s.ListenAndServe()
}Fifth approach – pure fasthttp server
func TestFastSer(t *testing.T) {
address := ":8001"
handler := func(ctx *fasthttp.RequestCtx) {
path := string(ctx.Path())
switch path {
case "/test":
ctx.SetBody([]byte("这是fasthttp创建服务的第一种语法"))
default:
ctx.SetBody([]byte(task.FunTester))
}
}
s := &fasthttp.Server{
Handler: handler,
Name: "FunTester server",
}
if err := s.ListenAndServe(address); err != nil {
log.Fatal("error in ListenAndServe", err.Error())
}
}Sixth approach – fasthttp with fasthttprouter
func TestFastSer2(t *testing.T) {
address := ":8001"
router := fasthttprouter.New()
router.GET("/test", func(ctx *fasthttp.RequestCtx) {
ctx.Response.SetBody([]byte("这是fasthttp创建server的第二种语法"))
})
router.GET("/", func(ctx *fasthttp.RequestCtx) {
ctx.Response.SetBody([]byte(task.FunTester))
})
fasthttp.ListenAndServe(address, router.Handler)
}These six demos illustrate the flexibility of Go's ecosystem: developers can choose a minimal net/http handler for maximum control, adopt a lightweight router like Echo or Gin for clearer routing, or leverage the high‑performance fasthttp library with or without a dedicated router. The examples also highlight differences in readability, configuration style, and how Go’s standard library compares to more opinionated Java frameworks.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
