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
Provenance
- Commit
c58e0d5- Tree
a06374e- Author
apriil15 <nlwkobe30@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
79a61b9
1 file changed+73 added−76 removed
modified:docs/doc.md+73 added-76 removed
448 unmodified lines449 ### How to write log file450 451 ```go452 func 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()455 456- // 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)459 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)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)462 463- 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+ })467 468- router.Run(":8080")468+ router.Run(":8080")469 }470 ```471 472 ### Custom Log Format42 unmodified lines515 Never colorize logs:516 517 ```go518 func main() {519- // Disable log's color520- gin.DisableConsoleColor()519+ // Disable log's color520+ gin.DisableConsoleColor()521 522- // 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()525 526- 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+ })529 530- router.Run(":8080")530+ router.Run(":8080")531 }532 ```533 534 Always colorize logs:535 536 ```go537 func main() {538- // Force log's color539- gin.ForceConsoleColor()538+ // Force log's color539+ gin.ForceConsoleColor()540 541- // 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()544 545- 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+ })548 549- router.Run(":8080")549+ router.Run(":8080")550 }551 ```552 553 ### Model binding and validation231 unmodified lines785 "github.com/gin-gonic/gin"786 )787 788 type 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 }795 796 func main() {797 route := gin.Default()5 unmodified lines803 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+ }814 815 c.String(http.StatusOK, "Success")816 }817 ```492 unmodified lines1310 main.go1311 1312 ```go1313 import (1314- "fmt"1315- "html/template"1316- "net/http"1317- "time"1314+ "fmt"1315+ "html/template"1316+ "net/http"1317+ "time"1318 1319- "github.com/gin-gonic/gin"1319+ "github.com/gin-gonic/gin"1320 )1321 1322 func 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 }1326 1327 func 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")1334 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- })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+ })1340 1341- router.Run(":8080")1341+ router.Run(":8080")1342 }1343 1344 ```1345 752 unmodified lines2098 ### Set and get a cookie2099 2100 ```go2101 import (2102- "fmt"2102+ "fmt"2103 2104- "github.com/gin-gonic/gin"2104+ "github.com/gin-gonic/gin"2105 )2106 2107 func main() {2108+ router := gin.Default()2109 2109- router := gin.Default()2110+ router.GET("/cookie", func(c *gin.Context) {2111 2111- router.GET("/cookie", func(c *gin.Context) {2112+ cookie, err := c.Cookie("gin_cookie")2113 2113- cookie, err := c.Cookie("gin_cookie")2114+ if err != nil {2115+ cookie = "NotSet"2116+ c.SetCookie("gin_cookie", "test", 3600, "/", "localhost", false, true)2117+ }2118 2115- 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+ })2121 2120- fmt.Printf("Cookie value: %s \n", cookie)2121- })2122-2123- router.Run()2122+ router.Run()2123 }2124 ```2125 2126 ## Don't trust all proxies20 unmodified lines2147 "github.com/gin-gonic/gin"2148 )2149 2150 func main() {2152-2151 router := gin.Default()2152 router.SetTrustedProxies([]string{"192.168.1.2"})2153 2154 router.GET("/", func(c *gin.Context) {18 unmodified lines2173 "github.com/gin-gonic/gin"2174 )2175 2176 func main() {2179-2177 router := gin.Default()2178 // Use predefined header gin.PlatformXXX2179 router.TrustedPlatform = gin.PlatformGoogleAppEngine2180 // Or set your own trusted request header for another trusted proxy service63 unmodified lines