mirror of https://github.com/gin-gonic/gin.git
Improves README.md
This commit is contained in:
parent
15216a0883
commit
d10e2b6c0d
51
README.md
51
README.md
|
@ -1,6 +1,6 @@
|
||||||
#Gin Web Framework
|
#Gin Web Framework
|
||||||
Gin is a web framework written in Golang. It features a martini-like API with much better performance, up to 40 times faster. If you need performance and good productivity, you will love Gin.
|
Gin is a web framework written in Golang. It features a martini-like API with much better performance, up to 40 times faster. If you need performance and good productivity, you will love Gin.
|
||||||
[Check out the official web site](http://gingonic.github.com)
|
[Check out the official web site](http://gin-gonic.github.io/gin/)
|
||||||
|
|
||||||
## Start using it
|
## Start using it
|
||||||
Run:
|
Run:
|
||||||
|
@ -18,14 +18,28 @@ import "github.com/gin-gonic/gin"
|
||||||
##API Examples
|
##API Examples
|
||||||
|
|
||||||
#### Create most basic PING/PONG HTTP endpoint
|
#### Create most basic PING/PONG HTTP endpoint
|
||||||
```
|
```go
|
||||||
|
import "github.com/gin-gonic/gin"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Creates a gin router + logger and recovery (crash-free) middlwares
|
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
r.GET("/ping", func(c *gin.Context){
|
r.GET("ping", func(c *gin.Context){
|
||||||
c.String("pong")
|
c.String("pong")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Listen and server on 0.0.0.0:8080
|
||||||
|
r.Run(":80")
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Using GET, POST, PUT, PATCH and DELETE
|
||||||
|
|
||||||
|
```go
|
||||||
|
func main() {
|
||||||
|
// Creates a gin router + logger and recovery (crash-free) middlwares
|
||||||
|
r := gin.Default()
|
||||||
|
|
||||||
|
r.GET("/someGet", getting)
|
||||||
r.POST("/somePost", posting)
|
r.POST("/somePost", posting)
|
||||||
r.PUT("/somePut", putting)
|
r.PUT("/somePut", putting)
|
||||||
r.DELETE("/someDelete", deleting)
|
r.DELETE("/someDelete", deleting)
|
||||||
|
@ -38,7 +52,7 @@ func main() {
|
||||||
|
|
||||||
#### Parameters in path
|
#### Parameters in path
|
||||||
|
|
||||||
```
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
|
@ -52,7 +66,7 @@ func main() {
|
||||||
|
|
||||||
|
|
||||||
#### Grouping routes
|
#### Grouping routes
|
||||||
```
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
|
@ -82,18 +96,18 @@ func main() {
|
||||||
|
|
||||||
Use
|
Use
|
||||||
|
|
||||||
```
|
```go
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
```
|
```
|
||||||
instead of
|
instead of
|
||||||
|
|
||||||
```
|
```go
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
#### Using middlewares
|
#### Using middlewares
|
||||||
```
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
// Creates a router without any middlware by default
|
// Creates a router without any middlware by default
|
||||||
r := gin.New()
|
r := gin.New()
|
||||||
|
@ -129,8 +143,8 @@ func main() {
|
||||||
|
|
||||||
|
|
||||||
#### JSON parsing and validation
|
#### JSON parsing and validation
|
||||||
```
|
|
||||||
|
|
||||||
|
```go
|
||||||
type LoginJSON struct {
|
type LoginJSON struct {
|
||||||
User string `json:"user" binding:"required"`
|
User string `json:"user" binding:"required"`
|
||||||
Password string `json:"password" binding:"required"`
|
Password string `json:"password" binding:"required"`
|
||||||
|
@ -158,7 +172,7 @@ func main() {
|
||||||
|
|
||||||
#### XML, and JSON rendering
|
#### XML, and JSON rendering
|
||||||
|
|
||||||
```
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
|
||||||
|
@ -189,7 +203,7 @@ func main() {
|
||||||
|
|
||||||
Using LoadHTMLTemplates()
|
Using LoadHTMLTemplates()
|
||||||
|
|
||||||
```
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
r.LoadHTMLTemplates("templates/*")
|
r.LoadHTMLTemplates("templates/*")
|
||||||
|
@ -202,7 +216,7 @@ func main() {
|
||||||
|
|
||||||
You can also use your own html template render
|
You can also use your own html template render
|
||||||
|
|
||||||
```
|
```go
|
||||||
import "html/template"
|
import "html/template"
|
||||||
func main() {
|
func main() {
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
|
@ -214,7 +228,7 @@ func main() {
|
||||||
|
|
||||||
#### Custom Middlewares
|
#### Custom Middlewares
|
||||||
|
|
||||||
```
|
```go
|
||||||
func Logger() gin.HandlerFunc {
|
func Logger() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
t : time.Now()
|
t : time.Now()
|
||||||
|
@ -242,6 +256,7 @@ func main() {
|
||||||
// it would print: "12345"
|
// it would print: "12345"
|
||||||
log.Println(example)
|
log.Println(example)
|
||||||
})
|
})
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -249,9 +264,9 @@ func main() {
|
||||||
|
|
||||||
#### Custom HTTP configuration
|
#### Custom HTTP configuration
|
||||||
|
|
||||||
Do not use the `Run()` method, instead use this:
|
Use `http.ListenAndServe()` directly, like this:
|
||||||
|
|
||||||
```
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
http.ListenAndServe(":8080", router)
|
http.ListenAndServe(":8080", router)
|
||||||
|
@ -259,7 +274,7 @@ func main() {
|
||||||
```
|
```
|
||||||
or
|
or
|
||||||
|
|
||||||
```
|
```go
|
||||||
func main() {
|
func main() {
|
||||||
router := gin.Default()
|
router := gin.Default()
|
||||||
|
|
||||||
|
@ -273,5 +288,3 @@ func main() {
|
||||||
s.ListenAndServe()
|
s.ListenAndServe()
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue