文章目录

监听多端口

自定义的struct绑定form-data

监听多端口

如何利用gin实现监听多端口

package main

import (

"log"

"net/http"

"time"

"github.com/gin-gonic/gin"

"golang.org/x/sync/errgroup"

)

var (

g errgroup.Group

)

func router01() http.Handler {

e := gin.New()

e.Use(gin.Recovery())

e.GET("/", func(c *gin.Context) {

c.JSON(

http.StatusOK,

gin.H{

"code": http.StatusOK,

"error": "Welcome server 01",

},

)

})

return e

}

func router02() http.Handler {

e := gin.New()

e.Use(gin.Recovery())

e.GET("/", func(c *gin.Context) {

c.JSON(

http.StatusOK,

gin.H{

"code": http.StatusOK,

"error": "Welcome server 02",

},

)

})

return e

}

func main() {

server01 := &http.Server{

Addr: ":8080",

Handler: router01(),

ReadTimeout: 5 * time.Second,

WriteTimeout: 10 * time.Second,

}

server02 := &http.Server{

Addr: ":8081",

Handler: router02(),

ReadTimeout: 5 * time.Second,

WriteTimeout: 10 * time.Second,

}

g.Go(func() error {

return server01.ListenAndServe()

})

g.Go(func() error {

return server02.ListenAndServe()

})

if err := g.Wait(); err != nil {

log.Fatal(err)

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

测试效果图如下:

 

自定义的struct绑定form-data

package main

import "github.com/gin-gonic/gin"

type StructA struct {

FieldA string `form:"field_a"`

}

type StructB struct {

NestedStruct StructA

FieldB string `form:"field_b"`

}

type StructC struct {

NestedStructPointer *StructA

FieldC string `form:"field_c"`

}

type StructD struct {

NestedAnonyStruct struct {

FieldX string `form:"field_x"`

}

FieldD string `form:"field_d"`

}

func GetDataB(c *gin.Context) {

var b StructB

c.Bind(&b)

c.JSON(200, gin.H{

"a": b.NestedStruct,

"b": b.FieldB,

})

}

func GetDataC(c *gin.Context) {

var b StructC

c.Bind(&b)

c.JSON(200, gin.H{

"a": b.NestedStructPointer,

"c": b.FieldC,

})

}

func GetDataD(c *gin.Context) {

var b StructD

c.Bind(&b)

c.JSON(200, gin.H{

"x": b.NestedAnonyStruct,

"d": b.FieldD,

})

}

func main() {

r := gin.Default()

r.GET("/getb", GetDataB)

r.GET("/getc", GetDataC)

r.GET("/getd", GetDataD)

r.Run()

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

直接看效果:

输入http://127.0.0.1:8080/getb?field_a=wyf&field_b=hhh

输出{"a":{"FieldA":"wyf"},"b":"hhh"}

输入http://localhost:8080/getc?field_a=hello&field_c=world

输出{"a":{"FieldA":"hello"},"c":"world"}

输入http://localhost:8080/getd?field_x=hello&field_d=worl

输出{"d":"worl","x":{"FieldX":"hello"}}

但是以下的格式是无法绑定的:

type StructX struct {

X struct {} `form:"name_x"` // HERE have form

}

type StructY struct {

Y StructX `form:"name_y"` // HERE hava form

}

type StructZ struct {

Z *StructZ `form:"name_z"` // HERE hava form

}

————————————————