Upgrade to Pro — share decks privately, control downloads, hide ads and more …

gRPC入門

 gRPC入門

Taiga Sakaguchi

November 19, 2022
Tweet

More Decks by Taiga Sakaguchi

Other Decks in Technology

Transcript

  1. type Server struct {} type User struct{ ID string }

    func (s *Server) GetUser(id string) *User {} type Client struct {} func (c *Client)DoSomething() { u := c.GetUser("userid") } 8
  2. REST だったら type Server struct {} func main() { r

    := gin.Default() r.GET("/user/:id", getUser) r.Run() } func getUser(c *gin.Context) {} type Client struct {} func (c *Client) DoSomething() { resp, err := http.Get("http://user/1") } 9
  3. syntax = "proto3"; service User { rpc GetUser(GetUserRequest) returns (GetUserResponse)

    {} } message GetUserRequest { string id = 1; } message GetUserResponse { string id = 1; string name = 2; } 12
  4. クライアント用の生成コード // ... type UserClient interface { GetUser(ctx context.Context, in

    *GetUserRequest, opts ...grpc.CallOption) (*GetUserResponse, error) } func NewUserClient(cc grpc.ClientConnInterface) UserClient { return &userClient{cc} } // ... 14
  5. 32