Skip to content

start

https://gin-gonic.com/zh-cn/

gin examples | https://github.com/gin-gonic/examples

quickstart

sh
mkdir gin-quickstart && cd gin-quickstart

go mod init gin-quickstart

go get -u github.com/gin-gonic/gin
go
package main

import "github.com/gin-gonic/gin"

func main() {
  router := gin.Default()
  router.GET("/ping", func(c *gin.Context) {
    c.JSON(200, gin.H{
      "message": "pong",
    })
  })
  router.Run() // 默认监听 0.0.0.0:8080
}
sh
go run main.go

配置

go
import "net/http"

func main() {
  router := gin.Default()

  s := &http.Server{
    Addr:           ":8080",
    Handler:        router,
    ReadTimeout:    10 * time.Second,
    WriteTimeout:   10 * time.Second,
    MaxHeaderBytes: 1 << 20,
  }
  s.ListenAndServe()
}