docs: update markdown format

This commit is contained in:
nlwkobe30 2022-12-23 00:44:37 +08:00
parent e868fd1d3d
commit 627abc77ff
1 changed files with 69 additions and 71 deletions

140
README.md
View File

@ -574,22 +574,22 @@ func main() {
```go ```go
func main() { func main() {
// Disable Console Color, you don't need console color when writing the logs to file. // Disable Console Color, you don't need console color when writing the logs to file.
gin.DisableConsoleColor() gin.DisableConsoleColor()
// Logging to a file. // Logging to a file.
f, _ := os.Create("gin.log") f, _ := os.Create("gin.log")
gin.DefaultWriter = io.MultiWriter(f) gin.DefaultWriter = io.MultiWriter(f)
// Use the following code if you need to write the logs to file and console at the same time. // Use the following code if you need to write the logs to file and console at the same time.
// gin.DefaultWriter = io.MultiWriter(f, os.Stdout) // gin.DefaultWriter = io.MultiWriter(f, os.Stdout)
router := gin.Default() router := gin.Default()
router.GET("/ping", func(c *gin.Context) { router.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong") c.String(http.StatusOK, "pong")
}) })
   router.Run(":8080")    router.Run(":8080")
} }
``` ```
@ -640,18 +640,18 @@ Never colorize logs:
```go ```go
func main() { func main() {
// Disable log's color // Disable log's color
gin.DisableConsoleColor() gin.DisableConsoleColor()
// Creates a gin router with default middleware: // Creates a gin router with default middleware:
// logger and recovery (crash-free) middleware // logger and recovery (crash-free) middleware
router := gin.Default() router := gin.Default()
router.GET("/ping", func(c *gin.Context) { router.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong") c.String(http.StatusOK, "pong")
}) })
router.Run(":8080") router.Run(":8080")
} }
``` ```
@ -659,18 +659,18 @@ Always colorize logs:
```go ```go
func main() { func main() {
// Force log's color // Force log's color
gin.ForceConsoleColor() gin.ForceConsoleColor()
// Creates a gin router with default middleware: // Creates a gin router with default middleware:
// logger and recovery (crash-free) middleware // logger and recovery (crash-free) middleware
router := gin.Default() router := gin.Default()
router.GET("/ping", func(c *gin.Context) { router.GET("/ping", func(c *gin.Context) {
c.String(http.StatusOK, "pong") c.String(http.StatusOK, "pong")
}) })
router.Run(":8080") router.Run(":8080")
} }
``` ```
@ -910,11 +910,11 @@ import (
) )
type Person struct { type Person struct {
Name string `form:"name"` Name string `form:"name"`
Address string `form:"address"` Address string `form:"address"`
Birthday time.Time `form:"birthday" time_format:"2006-01-02" time_utc:"1"` Birthday time.Time `form:"birthday" time_format:"2006-01-02" time_utc:"1"`
CreateTime time.Time `form:"createTime" time_format:"unixNano"` CreateTime time.Time `form:"createTime" time_format:"unixNano"`
UnixTime time.Time `form:"unixTime" time_format:"unix"` UnixTime time.Time `form:"unixTime" time_format:"unix"`
} }
func main() { func main() {
@ -928,13 +928,13 @@ func startPage(c *gin.Context) {
// If `GET`, only `Form` binding engine (`query`) used. // If `GET`, only `Form` binding engine (`query`) used.
// If `POST`, first checks the `content-type` for `JSON` or `XML`, then uses `Form` (`form-data`). // If `POST`, first checks the `content-type` for `JSON` or `XML`, then uses `Form` (`form-data`).
// See more at https://github.com/gin-gonic/gin/blob/master/binding/binding.go#L88 // See more at https://github.com/gin-gonic/gin/blob/master/binding/binding.go#L88
if c.ShouldBind(&person) == nil { if c.ShouldBind(&person) == nil {
log.Println(person.Name) log.Println(person.Name)
log.Println(person.Address) log.Println(person.Address)
log.Println(person.Birthday) log.Println(person.Birthday)
log.Println(person.CreateTime) log.Println(person.CreateTime)
log.Println(person.UnixTime) log.Println(person.UnixTime)
} }
c.String(http.StatusOK, "Success") c.String(http.StatusOK, "Success")
} }
@ -1435,12 +1435,12 @@ main.go
```go ```go
import ( import (
"fmt" "fmt"
"html/template" "html/template"
"net/http" "net/http"
"time" "time"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
func formatAsDate(t time.Time) string { func formatAsDate(t time.Time) string {
@ -1449,20 +1449,20 @@ func formatAsDate(t time.Time) string {
} }
func main() { func main() {
router := gin.Default() router := gin.Default()
router.Delims("{[{", "}]}") router.Delims("{[{", "}]}")
router.SetFuncMap(template.FuncMap{ router.SetFuncMap(template.FuncMap{
"formatAsDate": formatAsDate, "formatAsDate": formatAsDate,
}) })
router.LoadHTMLFiles("./testdata/template/raw.tmpl") router.LoadHTMLFiles("./testdata/template/raw.tmpl")
router.GET("/raw", func(c *gin.Context) { router.GET("/raw", func(c *gin.Context) {
c.HTML(http.StatusOK, "raw.tmpl", gin.H{ c.HTML(http.StatusOK, "raw.tmpl", gin.H{
"now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC), "now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),
}) })
}) })
router.Run(":8080") router.Run(":8080")
} }
``` ```
@ -2229,22 +2229,20 @@ import (
) )
func main() { func main() {
router := gin.Default()
router := gin.Default() router.GET("/cookie", func(c *gin.Context) {
cookie, err := c.Cookie("gin_cookie")
router.GET("/cookie", func(c *gin.Context) { if err != nil {
cookie = "NotSet"
c.SetCookie("gin_cookie", "test", 3600, "/", "localhost", false, true)
}
cookie, err := c.Cookie("gin_cookie") fmt.Printf("Cookie value: %s \n", cookie)
})
if err != nil { router.Run()
cookie = "NotSet"
c.SetCookie("gin_cookie", "test", 3600, "/", "localhost", false, true)
}
fmt.Printf("Cookie value: %s \n", cookie)
})
router.Run()
} }
``` ```