mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-22 01:12:16 +08:00
3.4 KiB
3.4 KiB
Gin Web Framework

Gin is a web framework written in Go (Golang). It features a martini-like API with much better performance, up to 40 times faster thanks to httprouter. If you need performance and good productivity, you will love Gin.
The key features of Gin are:
- Zero allocation router
- Fast
- Middleware support
- Crash-free
- JSON validation
- Routes grouping
- Error management
- Rendering built-in
- Extendable
For more features, please see the Gin website.
Getting start
To install Gin package, you need to install Go and set your Go workspace first.
- Download and install it:
$ go get -u github.com/gin-gonic/gin
- Import it in your code:
import "github.com/gin-gonic/gin"
- (Optional) Import
net/http
. This is required for example if using constants such ashttp.StatusOK
.
import "net/http"
Use a vendor tool like Govendor
go get
govendor
$ go get github.com/kardianos/govendor
- Create your project folder and
cd
inside
$ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"
- Vendor init your project and add gin
$ govendor init
$ govendor fetch github.com/gin-gonic/gin@v1.3
- Copy a starting template inside your project
$ curl https://raw.githubusercontent.com/gin-gonic/gin/master/examples/basic/main.go > main.go
- Run your project
$ go run main.go
Quick start
# assume the following codes in example.go file
$ cat example.go
package main
import "github.com/gin-gonic/gin"
func main() {
r := gin.Default()
r.GET("/ping", func(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
})
r.Run() // listen and serve on 0.0.0.0:8080
}
# run example.go and visit 0.0.0.0:8080/ping on browser
$ go run example.go
Documentation
All documentation is available on the Gin website(English | 简体中文 | 繁體中文 | にほんご).
Examplses
A number of ready-to-run examples demonstrating various use cases of Gin on the Gin examples repository.