Skip to content

Commit 92ba8e1

Browse files
authored
fix: typo (#3171)
1 parent 58303bd commit 92ba8e1

File tree

1 file changed

+48
-27
lines changed

1 file changed

+48
-27
lines changed

README.md

+48-27
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,16 @@ $ cat example.go
114114
```go
115115
package main
116116

117-
import "github.com/gin-gonic/gin"
117+
import (
118+
"net/http"
119+
120+
"github.com/gin-gonic/gin"
121+
)
118122

119123
func main() {
120124
r := gin.Default()
121125
r.GET("/ping", func(c *gin.Context) {
122-
c.JSON(200, gin.H{
126+
c.JSON(http.StatusOK, gin.H{
123127
"message": "pong",
124128
})
125129
})
@@ -300,7 +304,7 @@ func main() {
300304
message := c.PostForm("message")
301305
nick := c.DefaultPostForm("nick", "anonymous")
302306

303-
c.JSON(200, gin.H{
307+
c.JSON(http.StatusOK, gin.H{
304308
"status": "posted",
305309
"message": message,
306310
"nick": nick,
@@ -570,7 +574,7 @@ func main() {
570574

571575
router := gin.Default()
572576
router.GET("/ping", func(c *gin.Context) {
573-
c.String(200, "pong")
577+
c.String(http.StatusOK, "pong")
574578
})
575579

576580
   router.Run(":8080")
@@ -602,7 +606,7 @@ func main() {
602606
router.Use(gin.Recovery())
603607

604608
router.GET("/ping", func(c *gin.Context) {
605-
c.String(200, "pong")
609+
c.String(http.StatusOK, "pong")
606610
})
607611

608612
router.Run(":8080")
@@ -630,7 +634,7 @@ func main() {
630634
router := gin.Default()
631635

632636
router.GET("/ping", func(c *gin.Context) {
633-
c.String(200, "pong")
637+
c.String(http.StatusOK, "pong")
634638
})
635639

636640
router.Run(":8080")
@@ -649,7 +653,7 @@ func main() {
649653
router := gin.Default()
650654

651655
router.GET("/ping", func(c *gin.Context) {
652-
c.String(200, "pong")
656+
c.String(http.StatusOK, "pong")
653657
})
654658

655659
router.Run(":8080")
@@ -848,6 +852,7 @@ package main
848852

849853
import (
850854
"log"
855+
"net/http"
851856

852857
"github.com/gin-gonic/gin"
853858
)
@@ -870,7 +875,7 @@ func startPage(c *gin.Context) {
870875
log.Println(person.Name)
871876
log.Println(person.Address)
872877
}
873-
c.String(200, "Success")
878+
c.String(http.StatusOK, "Success")
874879
}
875880

876881
```
@@ -884,6 +889,7 @@ package main
884889

885890
import (
886891
"log"
892+
"net/http"
887893
"time"
888894

889895
"github.com/gin-gonic/gin"
@@ -916,7 +922,7 @@ func startPage(c *gin.Context) {
916922
log.Println(person.UnixTime)
917923
}
918924

919-
c.String(200, "Success")
925+
c.String(http.StatusOK, "Success")
920926
}
921927
```
922928

@@ -932,7 +938,11 @@ See the [detail information](https://github.com/gin-gonic/gin/issues/846).
932938
```go
933939
package main
934940

935-
import "github.com/gin-gonic/gin"
941+
import (
942+
"net/http"
943+
944+
"github.com/gin-gonic/gin"
945+
)
936946

937947
type Person struct {
938948
ID string `uri:"id" binding:"required,uuid"`
@@ -944,10 +954,10 @@ func main() {
944954
route.GET("/:name/:id", func(c *gin.Context) {
945955
var person Person
946956
if err := c.ShouldBindUri(&person); err != nil {
947-
c.JSON(400, gin.H{"msg": err.Error()})
957+
c.JSON(http.StatusBadRequest, gin.H{"msg": err.Error()})
948958
return
949959
}
950-
c.JSON(200, gin.H{"name": person.Name, "uuid": person.ID})
960+
c.JSON(http.StatusOK, gin.H{"name": person.Name, "uuid": person.ID})
951961
})
952962
route.Run(":8088")
953963
}
@@ -966,6 +976,8 @@ package main
966976

967977
import (
968978
"fmt"
979+
"net/http"
980+
969981
"github.com/gin-gonic/gin"
970982
)
971983

@@ -980,11 +992,11 @@ func main() {
980992
h := testHeader{}
981993

982994
if err := c.ShouldBindHeader(&h); err != nil {
983-
c.JSON(200, err)
995+
c.JSON(http.StatusOK, err)
984996
}
985997

986998
fmt.Printf("%#v\n", h)
987-
c.JSON(200, gin.H{"Rate": h.Rate, "Domain": h.Domain})
999+
c.JSON(http.StatusOK, gin.H{"Rate": h.Rate, "Domain": h.Domain})
9881000
})
9891001

9901002
r.Run()
@@ -1014,7 +1026,7 @@ type myForm struct {
10141026
func formHandler(c *gin.Context) {
10151027
var fakeForm myForm
10161028
c.ShouldBind(&fakeForm)
1017-
c.JSON(200, gin.H{"color": fakeForm.Colors})
1029+
c.JSON(http.StatusOK, gin.H{"color": fakeForm.Colors})
10181030
}
10191031

10201032
...
@@ -1219,14 +1231,14 @@ func main() {
12191231

12201232
// Serves unicode entities
12211233
r.GET("/json", func(c *gin.Context) {
1222-
c.JSON(200, gin.H{
1234+
c.JSON(http.StatusOK, gin.H{
12231235
"html": "<b>Hello, world!</b>",
12241236
})
12251237
})
12261238

12271239
// Serves literal characters
12281240
r.GET("/purejson", func(c *gin.Context) {
1229-
c.PureJSON(200, gin.H{
1241+
c.PureJSON(http.StatusOK, gin.H{
12301242
"html": "<b>Hello, world!</b>",
12311243
})
12321244
})
@@ -1473,7 +1485,7 @@ r.GET("/test", func(c *gin.Context) {
14731485
r.HandleContext(c)
14741486
})
14751487
r.GET("/test2", func(c *gin.Context) {
1476-
c.JSON(200, gin.H{"hello": "world"})
1488+
c.JSON(http.StatusOK, gin.H{"hello": "world"})
14771489
})
14781490
```
14791491

@@ -1626,6 +1638,7 @@ package main
16261638

16271639
import (
16281640
"log"
1641+
"net/http"
16291642

16301643
"github.com/gin-gonic/autotls"
16311644
"github.com/gin-gonic/gin"
@@ -1636,7 +1649,7 @@ func main() {
16361649

16371650
// Ping handler
16381651
r.GET("/ping", func(c *gin.Context) {
1639-
c.String(200, "pong")
1652+
c.String(http.StatusOK, "pong")
16401653
})
16411654

16421655
log.Fatal(autotls.Run(r, "example1.com", "example2.com"))
@@ -1650,6 +1663,7 @@ package main
16501663

16511664
import (
16521665
"log"
1666+
"net/http"
16531667

16541668
"github.com/gin-gonic/autotls"
16551669
"github.com/gin-gonic/gin"
@@ -1661,7 +1675,7 @@ func main() {
16611675

16621676
// Ping handler
16631677
r.GET("/ping", func(c *gin.Context) {
1664-
c.String(200, "pong")
1678+
c.String(http.StatusOK, "pong")
16651679
})
16661680

16671681
m := autocert.Manager{
@@ -1922,7 +1936,7 @@ type StructD struct {
19221936
func GetDataB(c *gin.Context) {
19231937
var b StructB
19241938
c.Bind(&b)
1925-
c.JSON(200, gin.H{
1939+
c.JSON(http.StatusOK, gin.H{
19261940
"a": b.NestedStruct,
19271941
"b": b.FieldB,
19281942
})
@@ -1931,7 +1945,7 @@ func GetDataB(c *gin.Context) {
19311945
func GetDataC(c *gin.Context) {
19321946
var b StructC
19331947
c.Bind(&b)
1934-
c.JSON(200, gin.H{
1948+
c.JSON(http.StatusOK, gin.H{
19351949
"a": b.NestedStructPointer,
19361950
"c": b.FieldC,
19371951
})
@@ -1940,7 +1954,7 @@ func GetDataC(c *gin.Context) {
19401954
func GetDataD(c *gin.Context) {
19411955
var b StructD
19421956
c.Bind(&b)
1943-
c.JSON(200, gin.H{
1957+
c.JSON(http.StatusOK, gin.H{
19441958
"x": b.NestedAnonyStruct,
19451959
"d": b.FieldD,
19461960
})
@@ -2090,6 +2104,7 @@ package main
20902104
import (
20912105
"html/template"
20922106
"log"
2107+
"net/http"
20932108

20942109
"github.com/gin-gonic/gin"
20952110
)
@@ -2118,7 +2133,7 @@ func main() {
21182133
log.Printf("Failed to push: %v", err)
21192134
}
21202135
}
2121-
c.HTML(200, "https", gin.H{
2136+
c.HTML(http.StatusOK, "https", gin.H{
21222137
"status": "success",
21232138
})
21242139
})
@@ -2274,10 +2289,16 @@ The `net/http/httptest` package is preferable way for HTTP testing.
22742289
```go
22752290
package main
22762291

2292+
import (
2293+
"net/http"
2294+
2295+
"github.com/gin-gonic/gin"
2296+
)
2297+
22772298
func setupRouter() *gin.Engine {
22782299
r := gin.Default()
22792300
r.GET("/ping", func(c *gin.Context) {
2280-
c.String(200, "pong")
2301+
c.String(http.StatusOK, "pong")
22812302
})
22822303
return r
22832304
}
@@ -2305,10 +2326,10 @@ func TestPingRoute(t *testing.T) {
23052326
router := setupRouter()
23062327

23072328
w := httptest.NewRecorder()
2308-
req, _ := http.NewRequest("GET", "/ping", nil)
2329+
req, _ := http.NewRequest(http.MethodGet, "/ping", nil)
23092330
router.ServeHTTP(w, req)
23102331

2311-
assert.Equal(t, 200, w.Code)
2332+
assert.Equal(t, http.StatusOK, w.Code)
23122333
assert.Equal(t, "pong", w.Body.String())
23132334
}
23142335
```

0 commit comments

Comments
 (0)