Skip to content
Myr

docs: update markdown format (#3446)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* docs: update markdown format * fix: resolve conflict * docs: update markdown format * docs: update * docs: update * Revert "docs: update" This reverts commit 82716193b753dbcad6fee85973790727b7a31ae5.

authored by apriil15 · committed by GitHub · parent 79a61b9 · Browse files · 1 file+73 added−76 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+73 added−76 removed

  • other:+73 added−76 removed

1 file+73 added−76 removed

modified:docs/doc.md+73 added-76 removed
448 unmodified lines
449### How to write log file450451```go452func main() {453 // Disable Console Color, you don't need console color when writing the logs to file.454 gin.DisableConsoleColor()453 // Disable Console Color, you don't need console color when writing the logs to file.454 gin.DisableConsoleColor()455456 // Logging to a file.457 f, _ := os.Create("gin.log")458 gin.DefaultWriter = io.MultiWriter(f)456 // Logging to a file.457 f, _ := os.Create("gin.log")458 gin.DefaultWriter = io.MultiWriter(f)459460 // Use the following code if you need to write the logs to file and console at the same time.461 // gin.DefaultWriter = io.MultiWriter(f, os.Stdout)460 // Use the following code if you need to write the logs to file and console at the same time.461 // gin.DefaultWriter = io.MultiWriter(f, os.Stdout)462463 router := gin.Default()464 router.GET("/ping", func(c *gin.Context) {465 c.String(http.StatusOK, "pong")466 })463 router := gin.Default()464 router.GET("/ping", func(c *gin.Context) {465 c.String(http.StatusOK, "pong")466 })467468    router.Run(":8080")468   router.Run(":8080")469}470```471472### Custom Log Format
42 unmodified lines
515Never colorize logs:516517```go518func main() {519 // Disable log's color520 gin.DisableConsoleColor()519 // Disable log's color520 gin.DisableConsoleColor()521522 // Creates a gin router with default middleware:523 // logger and recovery (crash-free) middleware524 router := gin.Default()522 // Creates a gin router with default middleware:523 // logger and recovery (crash-free) middleware524 router := gin.Default()525526 router.GET("/ping", func(c *gin.Context) {527 c.String(http.StatusOK, "pong")528 })526 router.GET("/ping", func(c *gin.Context) {527 c.String(http.StatusOK, "pong")528 })529530 router.Run(":8080")530 router.Run(":8080")531}532```533534Always colorize logs:535536```go537func main() {538 // Force log's color539 gin.ForceConsoleColor()538 // Force log's color539 gin.ForceConsoleColor()540541 // Creates a gin router with default middleware:542 // logger and recovery (crash-free) middleware543 router := gin.Default()541 // Creates a gin router with default middleware:542 // logger and recovery (crash-free) middleware543 router := gin.Default()544545 router.GET("/ping", func(c *gin.Context) {546 c.String(http.StatusOK, "pong")547 })545 router.GET("/ping", func(c *gin.Context) {546 c.String(http.StatusOK, "pong")547 })548549 router.Run(":8080")549 router.Run(":8080")550}551```552553### Model binding and validation
231 unmodified lines
785 "github.com/gin-gonic/gin"786)787788type Person struct {789 Name string `form:"name"`790 Address string `form:"address"`791 Birthday time.Time `form:"birthday" time_format:"2006-01-02" time_utc:"1"`792 CreateTime time.Time `form:"createTime" time_format:"unixNano"`793 UnixTime time.Time `form:"unixTime" time_format:"unix"`789 Name string `form:"name"`790 Address string `form:"address"`791 Birthday time.Time `form:"birthday" time_format:"2006-01-02" time_utc:"1"`792 CreateTime time.Time `form:"createTime" time_format:"unixNano"`793 UnixTime time.Time `form:"unixTime" time_format:"unix"`794}795796func main() {797 route := gin.Default()
5 unmodified lines
803 var person Person804 // If `GET`, only `Form` binding engine (`query`) used.805 // If `POST`, first checks the `content-type` for `JSON` or `XML`, then uses `Form` (`form-data`).806 // See more at https://github.com/gin-gonic/gin/blob/master/binding/binding.go#L88807 if c.ShouldBind(&person) == nil {808 log.Println(person.Name)809 log.Println(person.Address)810 log.Println(person.Birthday)811 log.Println(person.CreateTime)812 log.Println(person.UnixTime)813 }807 if c.ShouldBind(&person) == nil {808 log.Println(person.Name)809 log.Println(person.Address)810 log.Println(person.Birthday)811 log.Println(person.CreateTime)812 log.Println(person.UnixTime)813 }814815 c.String(http.StatusOK, "Success")816}817```
492 unmodified lines
1310main.go13111312```go1313import (1314 "fmt"1315 "html/template"1316 "net/http"1317 "time"1314 "fmt"1315 "html/template"1316 "net/http"1317 "time"13181319 "github.com/gin-gonic/gin"1319 "github.com/gin-gonic/gin"1320)13211322func formatAsDate(t time.Time) string {1323 year, month, day := t.Date()1324 return fmt.Sprintf("%d/%02d/%02d", year, month, day)1323 year, month, day := t.Date()1324 return fmt.Sprintf("%d/%02d/%02d", year, month, day)1325}13261327func main() {1328 router := gin.Default()1329 router.Delims("{[{", "}]}")1330 router.SetFuncMap(template.FuncMap{1331 "formatAsDate": formatAsDate,1332 })1333 router.LoadHTMLFiles("./testdata/template/raw.tmpl")1328 router := gin.Default()1329 router.Delims("{[{", "}]}")1330 router.SetFuncMap(template.FuncMap{1331 "formatAsDate": formatAsDate,1332 })1333 router.LoadHTMLFiles("./testdata/template/raw.tmpl")13341335 router.GET("/raw", func(c *gin.Context) {1336 c.HTML(http.StatusOK, "raw.tmpl", gin.H{1337 "now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),1338 })1339 })1335 router.GET("/raw", func(c *gin.Context) {1336 c.HTML(http.StatusOK, "raw.tmpl", gin.H{1337 "now": time.Date(2017, 07, 01, 0, 0, 0, 0, time.UTC),1338 })1339 })13401341 router.Run(":8080")1341 router.Run(":8080")1342}13431344```1345
752 unmodified lines
2098### Set and get a cookie20992100```go2101import (2102 "fmt"2102 "fmt"21032104 "github.com/gin-gonic/gin"2104 "github.com/gin-gonic/gin"2105)21062107func main() {2108 router := gin.Default()21092109 router := gin.Default()2110 router.GET("/cookie", func(c *gin.Context) {21112111 router.GET("/cookie", func(c *gin.Context) {2112 cookie, err := c.Cookie("gin_cookie")21132113 cookie, err := c.Cookie("gin_cookie")2114 if err != nil {2115 cookie = "NotSet"2116 c.SetCookie("gin_cookie", "test", 3600, "/", "localhost", false, true)2117 }21182115 if err != nil {2116 cookie = "NotSet"2117 c.SetCookie("gin_cookie", "test", 3600, "/", "localhost", false, true)2118 }2119 fmt.Printf("Cookie value: %s \n", cookie)2120 })21212120 fmt.Printf("Cookie value: %s \n", cookie)2121 })21222123 router.Run()2122 router.Run()2123}2124```21252126## Don't trust all proxies
20 unmodified lines
2147 "github.com/gin-gonic/gin"2148)21492150func main() {21522151 router := gin.Default()2152 router.SetTrustedProxies([]string{"192.168.1.2"})21532154 router.GET("/", func(c *gin.Context) {
18 unmodified lines
2173 "github.com/gin-gonic/gin"2174)21752176func main() {21792177 router := gin.Default()2178 // Use predefined header gin.PlatformXXX2179 router.TrustedPlatform = gin.PlatformGoogleAppEngine2180 // Or set your own trusted request header for another trusted proxy service
63 unmodified lines