What is Gin?
Gin is an open-source framework written in Go (Golang)
. At the moment, it has 55.6K stars on GitHub , and it’s similar to Martini
but with better performance API; route parsing is nearly 40 times faster due to its use of httprouter
.
Installation
Create An Empty Directory
$ mkdir gin-demo
Using Go Module Initialization
$ cd gin-demo
$ go mod init go-demo
go: creating new go.mod: module go-demo
Installation
# Execute in the newly created directory
$ go get -u github.com/gin-gonic/gin
go: downloading github.com/gin-gonic/gin v1.7.7
go: downloading github.com/gin-contrib/sse v0.1.0
go: downloading github.com/mattn/go-isatty v0.0.12
go: downloading github.com/json-iterator/go v1.1.9
go: downloading github.com/golang/protobuf v1.3.3
go: downloading github.com/ugorji/go/codec v1.1.7
go: downloading gopkg.in/yaml.v2 v2.2.8
go: downloading github.com/go-playground/validator/v10 v10.4.1
go: downloading github.com/ugorji/go v1.1.7
go: downloading github.com/json-iterator/go v1.1.12
....omitted
Start the service
Code
package main
import (
"github.com/gin-gonic/gin"
)
func main () {
// Create a default routing engine
engine := gin . Default ()
// Register a route and set up an anonymous handler to return data in JSON format
engine . GET ( "/" , func ( ctx * gin . Context ) {
ctx . JSON ( 200 , gin . H {
"msg" : "request succeeded" ,
})
})
// Start the service and listen on port 9090,
// If not filled, the default listener is 0.0.0.0:8080
_ = engine . Run ( ":9090" )
}
Running
$ go run main.go
[ GIN-debug] [ WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[ GIN-debug] [ WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env : export GIN_MODE = release
- using code: gin.SetMode( gin.ReleaseMode)
[ GIN-debug] GET / -- > main.main.func1 ( 3 handlers)
[ GIN-debug] [ WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[ GIN-debug] Listening and serving HTTP on :9090
Access
$ go run main.go
[ GIN-debug] [ WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[ GIN-debug] [ WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env : export GIN_MODE = release
- using code: gin.SetMode( gin.ReleaseMode)
[ GIN-debug] GET / -- > main.main.func1 ( 3 handlers)
[ GIN-debug] [ WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[ GIN-debug] Listening and serving HTTP on :9090
[ GIN] 2022/02/17 - 18:55:17 | 200 | 246.194µs | 127.0.0.1 | GET "/"
[ GIN] 2022/02/17 - 18:55:17 | 404 | 639ns | 127.0.0.1 | GET "/favicon.ico"
[ GIN] 2022/02/17 - 18:55:50 | 200 | 94.146µs | 127.0.0.1 | GET "/"
[ GIN] 2022/02/17 - 18:59:02 | 200 | 62.582µs | 127.0.0.1 | GET "/"
[ GIN] 2022/02/17 - 18:59:08 | 200 | 29.111µs | 127.0.0.1 | GET "/"