Slide 9
Slide 9 text
実装例: バックエンド (Go)
同じContractから並行実装
// 1.
サービス実装(.proto
から生成されたインターフェース)
type GreetServer struct{}
func (s *GreetServer) Greet(ctx context.Context, req *connect.Request[greetv1.GreetRequest]) (*connect.Response[greetv1.
return connect.NewResponse(&greetv1.GreetResponse{
Greeting: fmt.Sprintf("
こんにちは、%s
さん", req.Msg.Name),
}), nil
}
// 2.
ハンドラー作成(Connect/gRPC/gRPC-Web
全対応)
mux := http.NewServeMux()
path, handler := greetv1connect.NewGreetServiceHandler(&GreetServer{})
mux.Handle(path, handler)
// 3.
サーバー起動
http.ListenAndServe(":8080", mux)
✨ ポイント: フロントエンドと同じ.protoから生
成、型の不一致は起こりません