Skip to content

Commit 8c9ecfb

Browse files
committed
style: switch tabs to spaces
1 parent 318888e commit 8c9ecfb

35 files changed

+836
-835
lines changed

src/content/docs/zh-tw/docs/examples/binding-and-validation.md

Lines changed: 62 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -24,75 +24,75 @@ Gin 使用 [**go-playground/validator/v10**](https://github.com/go-playground/va
2424
```go
2525
// 從 JSON 綁定
2626
type Login struct {
27-
User string `form:"user" json:"user" xml:"user" binding:"required"`
28-
Password string `form:"password" json:"password" xml:"password" binding:"required"`
27+
User string `form:"user" json:"user" xml:"user" binding:"required"`
28+
Password string `form:"password" json:"password" xml:"password" binding:"required"`
2929
}
3030

3131
import (
32-
"net/http"
32+
"net/http"
3333

34-
"github.com/gin-gonic/gin"
34+
"github.com/gin-gonic/gin"
3535
)
3636

3737
func main() {
38-
router := gin.Default()
39-
40-
// 綁定 JSON 的範例 ({"user": "manu", "password": "123"})
41-
router.POST("/loginJSON", func(c *gin.Context) {
42-
var json Login
43-
if err := c.ShouldBindJSON(&json); err != nil {
44-
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
45-
return
46-
}
47-
48-
if json.User != "manu" || json.Password != "123" {
49-
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
50-
return
51-
}
52-
53-
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
54-
})
55-
56-
// 綁定 XML 的範例 (
57-
// <?xml version="1.0" encoding="UTF-8"?>
58-
// <root>
59-
// <user>manu</user>
60-
// <password>123</user>
61-
// </root>)
62-
router.POST("/loginXML", func(c *gin.Context) {
63-
var xml Login
64-
if err := c.ShouldBindXML(&xml); err != nil {
65-
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
66-
return
67-
}
68-
69-
if xml.User != "manu" || xml.Password != "123" {
70-
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
71-
return
72-
}
73-
74-
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
75-
})
76-
77-
// 綁定 HTML 表單的範例 (user=manu&password=123)
78-
router.POST("/loginForm", func(c *gin.Context) {
79-
var form Login
80-
// 這將根據 content-type 標頭推斷要使用的綁定器。
81-
if err := c.ShouldBind(&form); err != nil {
82-
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
83-
return
84-
}
85-
86-
if form.User != "manu" || form.Password != "123" {
87-
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
88-
return
89-
}
90-
91-
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
92-
})
93-
94-
// 在 0.0.0.0:8080 上監聽並提供服務
95-
router.Run(":8080")
38+
router := gin.Default()
39+
40+
// 綁定 JSON 的範例 ({"user": "manu", "password": "123"})
41+
router.POST("/loginJSON", func(c *gin.Context) {
42+
var json Login
43+
if err := c.ShouldBindJSON(&json); err != nil {
44+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
45+
return
46+
}
47+
48+
if json.User != "manu" || json.Password != "123" {
49+
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
50+
return
51+
}
52+
53+
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
54+
})
55+
56+
// 綁定 XML 的範例 (
57+
// <?xml version="1.0" encoding="UTF-8"?>
58+
// <root>
59+
// <user>manu</user>
60+
// <password>123</user>
61+
// </root>)
62+
router.POST("/loginXML", func(c *gin.Context) {
63+
var xml Login
64+
if err := c.ShouldBindXML(&xml); err != nil {
65+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
66+
return
67+
}
68+
69+
if xml.User != "manu" || xml.Password != "123" {
70+
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
71+
return
72+
}
73+
74+
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
75+
})
76+
77+
// 綁定 HTML 表單的範例 (user=manu&password=123)
78+
router.POST("/loginForm", func(c *gin.Context) {
79+
var form Login
80+
// 這將根據 content-type 標頭推斷要使用的綁定器。
81+
if err := c.ShouldBind(&form); err != nil {
82+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
83+
return
84+
}
85+
86+
if form.User != "manu" || form.Password != "123" {
87+
c.JSON(http.StatusUnauthorized, gin.H{"status": "unauthorized"})
88+
return
89+
}
90+
91+
c.JSON(http.StatusOK, gin.H{"status": "you are logged in"})
92+
})
93+
94+
// 在 0.0.0.0:8080 上監聽並提供服務
95+
router.Run(":8080")
9696
}
9797
```
9898

src/content/docs/zh-tw/docs/examples/custom-http-config.md

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ title: "自訂 HTTP 組態"
88
import "net/http"
99

1010
func main() {
11-
router := gin.Default()
12-
http.ListenAndServe(":8080", router)
11+
router := gin.Default()
12+
http.ListenAndServe(":8080", router)
1313
}
1414
```
15+
1516
1617

1718
```go
@@ -23,15 +24,15 @@ import (
2324
)
2425

2526
func main() {
26-
router := gin.Default()
27-
28-
s := &http.Server{
29-
Addr: ":8080",
30-
Handler: router,
31-
ReadTimeout: 10 * time.Second,
32-
WriteTimeout: 10 * time.Second,
33-
MaxHeaderBytes: 1 << 20,
34-
}
35-
s.ListenAndServe()
27+
router := gin.Default()
28+
29+
s := &http.Server{
30+
Addr: ":8080",
31+
Handler: router,
32+
ReadTimeout: 10 * time.Second,
33+
WriteTimeout: 10 * time.Second,
34+
MaxHeaderBytes: 1 << 20,
35+
}
36+
s.ListenAndServe()
3637
}
3738
```

src/content/docs/zh-tw/docs/examples/custom-middleware.md

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,45 @@ title: "自訂中介軟體"
44

55
```go
66
import (
7-
"log"
8-
"time"
7+
"log"
8+
"time"
99

10-
"github.com/gin-gonic/gin"
10+
"github.com/gin-gonic/gin"
1111
)
1212

1313
func Logger() gin.HandlerFunc {
14-
return func(c *gin.Context) {
15-
t := time.Now()
14+
return func(c *gin.Context) {
15+
t := time.Now()
1616

17-
// 設定範例變數
18-
c.Set("example", "12345")
17+
// 設定範例變數
18+
c.Set("example", "12345")
1919

20-
// 請求前
20+
// 請求前
2121

22-
c.Next()
22+
c.Next()
2323

24-
// 請求後
25-
latency := time.Since(t)
26-
log.Print(latency)
24+
// 請求後
25+
latency := time.Since(t)
26+
log.Print(latency)
2727

28-
// 存取我們正在傳送的狀態
29-
status := c.Writer.Status()
30-
log.Println(status)
31-
}
28+
// 存取我們正在傳送的狀態
29+
status := c.Writer.Status()
30+
log.Println(status)
31+
}
3232
}
3333

3434
func main() {
35-
r := gin.New()
36-
r.Use(Logger())
35+
r := gin.New()
36+
r.Use(Logger())
3737

38-
r.GET("/test", func(c *gin.Context) {
39-
example := c.MustGet("example").(string)
38+
r.GET("/test", func(c *gin.Context) {
39+
example := c.MustGet("example").(string)
4040

41-
// 它會印出:「12345」
42-
log.Println(example)
43-
})
41+
// 它會印出:「12345」
42+
log.Println(example)
43+
})
4444

45-
// 在 0.0.0.0:8080 上監聽並提供服務
46-
r.Run(":8080")
45+
// 在 0.0.0.0:8080 上監聽並提供服務
46+
r.Run(":8080")
4747
}
4848
```

src/content/docs/zh-tw/docs/examples/custom-validators.md

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,52 +8,52 @@ title: "自訂驗證器"
88
package main
99

1010
import (
11-
"net/http"
12-
"reflect"
13-
"time"
11+
"net/http"
12+
"reflect"
13+
"time"
1414

15-
"github.com/gin-gonic/gin"
16-
"github.com/gin-gonic/gin/binding"
17-
"github.com/go-playground/validator/v10"
15+
"github.com/gin-gonic/gin"
16+
"github.com/gin-gonic/gin/binding"
17+
"github.com/go-playground/validator/v10"
1818
)
1919

2020
// Booking 包含已綁定和已驗證的資料。
2121
type Booking struct {
22-
CheckIn time.Time `form:"check_in" binding:"required,bookabledate" time_format:"2006-01-02"`
23-
CheckOut time.Time `form:"check_out" binding:"required,gtfield=CheckIn,bookabledate" time_format:"2006-01-02"`
22+
CheckIn time.Time `form:"check_in" binding:"required,bookabledate" time_format:"2006-01-02"`
23+
CheckOut time.Time `form:"check_out" binding:"required,gtfield=CheckIn,bookabledate" time_format:"2006-01-02"`
2424
}
2525

2626
func bookableDate(
27-
v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value,
28-
field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string,
27+
v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value,
28+
field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string,
2929
) bool {
30-
if date, ok := field.Interface().(time.Time); ok {
31-
today := time.Now()
32-
if today.Year() > date.Year() || today.YearDay() > date.YearDay() {
33-
return false
34-
}
35-
}
36-
return true
30+
if date, ok := field.Interface().(time.Time); ok {
31+
today := time.Now()
32+
if today.Year() > date.Year() || today.YearDay() > date.YearDay() {
33+
return false
34+
}
35+
}
36+
return true
3737
}
3838

3939
func main() {
40-
route := gin.Default()
40+
route := gin.Default()
4141

42-
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
43-
v.RegisterValidation("bookabledate", bookableDate)
44-
}
42+
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
43+
v.RegisterValidation("bookabledate", bookableDate)
44+
}
4545

46-
route.GET("/bookable", getBookable)
47-
route.Run(":8085")
46+
route.GET("/bookable", getBookable)
47+
route.Run(":8085")
4848
}
4949

5050
func getBookable(c *gin.Context) {
51-
var b Booking
52-
if err := c.ShouldBindWith(&b, binding.Query); err == nil {
53-
c.JSON(http.StatusOK, gin.H{"message": "預訂日期有效!"})
54-
} else {
55-
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
56-
}
51+
var b Booking
52+
if err := c.ShouldBindWith(&b, binding.Query); err == nil {
53+
c.JSON(http.StatusOK, gin.H{"message": "預訂日期有效!"})
54+
} else {
55+
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
56+
}
5757
}
5858
```
5959

src/content/docs/zh-tw/docs/examples/define-format-for-the-log-of-routes.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,31 @@ title: "定義路由日誌的格式"
1313
在下面的範例中,我們使用標準日誌套件記錄所有路由,但您也可以使用適合您需求的其他日誌工具。
1414
```go
1515
import (
16-
"log"
17-
"net/http"
16+
"log"
17+
"net/http"
1818

19-
"github.com/gin-gonic/gin"
19+
"github.com/gin-gonic/gin"
2020
)
2121

2222
func main() {
23-
router := gin.Default()
24-
gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
25-
log.Printf("endpoint %v %v %v %v\n", httpMethod, absolutePath, handlerName, nuHandlers)
26-
}
23+
router := gin.Default()
24+
gin.DebugPrintRouteFunc = func(httpMethod, absolutePath, handlerName string, nuHandlers int) {
25+
log.Printf("endpoint %v %v %v %v\n", httpMethod, absolutePath, handlerName, nuHandlers)
26+
}
2727

28-
router.POST("/foo", func(c *gin.Context) {
29-
c.JSON(http.StatusOK, "foo")
30-
})
28+
router.POST("/foo", func(c *gin.Context) {
29+
c.JSON(http.StatusOK, "foo")
30+
})
3131

32-
router.GET("/bar", func(c *gin.Context) {
33-
c.JSON(http.StatusOK, "bar")
34-
})
32+
router.GET("/bar", func(c *gin.Context) {
33+
c.JSON(http.StatusOK, "bar")
34+
})
3535

36-
router.GET("/status", func(c *gin.Context) {
37-
c.JSON(http.StatusOK, "ok")
38-
})
36+
router.GET("/status", func(c *gin.Context) {
37+
c.JSON(http.StatusOK, "ok")
38+
})
3939

40-
// 在 http://0.0.0.0:8080 上監聽並提供服務
41-
router.Run()
40+
// 在 http://0.0.0.0:8080 上監聽並提供服務
41+
router.Run()
4242
}
4343
```

0 commit comments

Comments
 (0)