From 83f686aaae9b0cd27ead1887fcd7ad77673238bc Mon Sep 17 00:00:00 2001 From: baytan0720 Date: Wed, 4 Oct 2023 03:44:37 +0800 Subject: [PATCH] update example --- README.md | 15 ++++++++++++++- example/main.go | 2 +- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 436bf0d..1cd7930 100644 --- a/README.md +++ b/README.md @@ -80,21 +80,34 @@ func main() { e := gin.Default(&Server{}) proto.RegisterHelloServer(e, e.Srv.(*Server)) + e.Use(gin.StoreRequestIntoKeys()) + e.Handle("Ping", func(c *gin.Context) { var req *proto.PingReq c.BindRequest(&req) fmt.Printf("ID: %s\n", req.Id) + fmt.Printf("ID: %s\n", c.GetString("Id")) + fmt.Printf("ID: %s\n", c.Req.GetField("Id")) c.Response(&proto.PongRes{ Status: 1, }) + + // another way to response: + // + //c.ResponseField("Status", int32(1)) + // + //c.ResponseFields(gin.H{ + // "Status": int32(1), + //}) }) - panic(e.Run()) // listen and serve on 0.0.0.0:8080 + e.Run() // listen and serve on 8080 } func (s *Server) Ping(ctx context.Context, req *proto.PingReq) (*proto.PongRes, error) { return &proto.PongRes{}, nil } + ``` You just need to define a struct and implement the interface `proto.HelloServer` without any logic. diff --git a/example/main.go b/example/main.go index 0853d6e..aa31ddd 100644 --- a/example/main.go +++ b/example/main.go @@ -37,7 +37,7 @@ func main() { //}) }) - panic(e.Run()) // listen and serve on 8080 + e.Run() // listen and serve on 8080 } func (s *Server) Ping(ctx context.Context, req *proto.PingReq) (*proto.PongRes, error) {