github.com/micro/go-micro/examples@v0.0.0-20210105173217-bf4ab679e18b/client/codegen/README.md (about) 1 # Code Generation [Experimental] 2 3 We're experimenting with code generation to reduce the amount of boiler plate code written. 4 5 ## Example 6 7 Going from this 8 ```golang 9 req := client.NewRequest("go.micro.srv.example", "Example.Call", &example.Request{ 10 Name: "John", 11 }) 12 13 rsp := &example.Response{} 14 15 if err := client.Call(context.Background(), req, rsp); err != nil { 16 return err 17 } 18 ``` 19 20 To 21 22 ```golang 23 rsp, err := cl.Call(context.Background(), &example.Request{Name: "John"}) 24 if err != nil { 25 return err 26 } 27 ``` 28 29 ## Generation of stub code for the example service 30 31 ```shell 32 go get github.com/micro/protobuf/protoc-gen-go 33 cd examples/server/proto/example 34 protoc --go_out=plugins=micro:. example.proto 35 ``` 36 37 Look at examples/server/proto/example/example.pb.go 38 to see the generated code. 39 40 ## Guide 41 42 ### Download the protoc-gen-go code 43 44 ```shell 45 go get github.com/micro/protobuf/protoc-gen-go 46 ``` 47 48 ### Define your proto service. 49 50 hello.proto 51 ```shell 52 syntax = "proto3"; 53 54 // package name is used as the service name for discovery 55 // if service name is not passed in when initialising the 56 // client 57 package go.micro.srv.greeter; 58 59 service Say { 60 rpc Hello(Request) returns (Response) {} 61 } 62 63 message Request { 64 optional string name = 1; 65 } 66 67 message Response { 68 optional string msg = 1; 69 } 70 ``` 71 72 **Note: Remember to set package name in the proto, it's used to generate 73 the service for discovery.** 74 75 ### Generate code 76 77 ```shell 78 protoc --go_out=plugins=micro:. hello.proto 79 ``` 80 81 ### Generated code 82 83 ```shell 84 // Client API for Say service 85 86 type SayClient interface { 87 Hello(ctx context.Context, in *Request) (*Response, error) 88 } 89 90 type sayClient struct { 91 c client.Client 92 serviceName string 93 } 94 95 func NewSayClient(serviceName string, c client.Client) SayClient { 96 if c == nil { 97 c = client.NewClient() 98 } 99 if len(serviceName) == 0 { 100 serviceName = "go.micro.srv.greeter" 101 } 102 return &sayClient{ 103 c: c, 104 serviceName: serviceName, 105 } 106 } 107 108 func (c *sayClient) Hello(ctx context.Context, in *Request) (*Response, error) { 109 req := c.c.NewRequest(c.serviceName, "Say.Hello", in) 110 out := new(Response) 111 err := c.c.Call(ctx, req, out) 112 if err != nil { 113 return nil, err 114 } 115 return out, nil 116 } 117 118 // Server API for Say service 119 120 type SayHandler interface { 121 Hello(context.Context, *Request, *Response) error 122 } 123 124 func RegisterSayHandler(s server.Server, hdlr SayHandler) { 125 s.Handle(s.NewHandler(hdlr)) 126 } 127 ``` 128 129 ### Use the client 130 ```golang 131 132 import ( 133 "fmt" 134 135 "context" 136 "github.com/micro/go-micro/v2/client" 137 hello "path/to/hello/proto" 138 ) 139 140 func main() { 141 cl := hello.NewSayClient("go.micro.srv.greeter", client.DefaultClient) 142 // alternative initialisation 143 // cl := hello.NewSayClient("", nil) 144 145 rsp, err := cl.Hello(contex.Background(), &hello.Request{"Name": "John"}) 146 if err != nil { 147 fmt.Println(err) 148 } 149 } 150 ```