mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-22 09:34:33 +08:00
120 lines
3.4 KiB
Markdown
120 lines
3.4 KiB
Markdown
# Gin Web Framework
|
|
|
|
<img align="right" width="159px" src="https://raw.githubusercontent.com/gin-gonic/logo/master/color.png">
|
|
|
|
[](https://travis-ci.org/gin-gonic/gin)
|
|
[](https://codecov.io/gh/gin-gonic/gin)
|
|
[](https://goreportcard.com/report/github.com/gin-gonic/gin)
|
|
[](https://godoc.org/github.com/gin-gonic/gin)
|
|
[](https://gitter.im/gin-gonic/gin?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
|
[](https://sourcegraph.com/github.com/gin-gonic/gin?badge)
|
|
[](https://www.codetriage.com/gin-gonic/gin)
|
|
[](https://github.com/gin-gonic/gin/releases)
|
|
|
|
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](https://github.com/julienschmidt/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](https://gin-gonic.com/).
|
|
|
|
## Getting start
|
|
|
|
To install Gin package, you need to install Go and set your Go workspace first.
|
|
|
|
1. Download and install it:
|
|
|
|
```sh
|
|
$ go get -u github.com/gin-gonic/gin
|
|
```
|
|
|
|
2. Import it in your code:
|
|
|
|
```go
|
|
import "github.com/gin-gonic/gin"
|
|
```
|
|
|
|
3. (Optional) Import `net/http`. This is required for example if using constants such as `http.StatusOK`.
|
|
|
|
```go
|
|
import "net/http"
|
|
```
|
|
|
|
### Use a vendor tool like [Govendor](https://github.com/kardianos/govendor)
|
|
|
|
1. `go get` govendor
|
|
|
|
```sh
|
|
$ go get github.com/kardianos/govendor
|
|
```
|
|
2. Create your project folder and `cd` inside
|
|
|
|
```sh
|
|
$ mkdir -p $GOPATH/src/github.com/myusername/project && cd "$_"
|
|
```
|
|
|
|
3. Vendor init your project and add gin
|
|
|
|
```sh
|
|
$ govendor init
|
|
$ govendor fetch github.com/gin-gonic/gin@v1.3
|
|
```
|
|
|
|
4. Copy a starting template inside your project
|
|
|
|
```sh
|
|
$ curl https://raw.githubusercontent.com/gin-gonic/examples/master/basic/main.go > main.go
|
|
```
|
|
|
|
5. Run your project
|
|
|
|
```sh
|
|
$ go run main.go
|
|
```
|
|
|
|
## Quick start
|
|
|
|
```sh
|
|
# assume the following codes in example.go file
|
|
$ cat example.go
|
|
```
|
|
|
|
```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](https://gin-gonic.com/docs/) | [简体中文](https://gin-gonic.com/zh-cn/docs/) | [繁體中文](https://gin-gonic.com/zh-tw/docs/) | [にほんご](https://gin-gonic.com/ja/docs/)).
|
|
|
|
## Examplses
|
|
|
|
A number of ready-to-run examples demonstrating various use cases of Gin on the [Gin examples](https://github.com/gin-gonic/examples) repository.
|
|
|