ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] ## HelloWorld 不使用 micro 的脚手架工具的一个示例 <details> <summary> client.go</summary> ``` package main import ( "cap/proto/cap/imooc" "context" "fmt" "github.com/asim/go-micro/v3" ) func main() { service:=micro.NewService( micro.Name("cap.imooc.client"), ) service.Init() capImooc:=imooc.NewCapService("cap.imooc.server",service.Client()) resp, err := capImooc.SayHello(context.TODO(), &imooc.SayRequest{Message: "hello world"}) if err != nil { panic(err) } fmt.Printf("%+v\n", resp) } ``` </details> <br /> <details> <summary> server.go </summary> ``` package main import ( "cap/proto/cap/imooc" "context" "fmt" "github.com/asim/go-micro/v3" ) type CapServer struct {} func (s *CapServer) SayHello(ctx context.Context,req *imooc.SayRequest,resp *imooc.SayResponse)error{ resp.Answer=fmt.Sprintf("我们的口号是%s",req.GetMessage()) return nil } func main() { service :=micro.NewService( micro.Name("cap.imooc.server"), ) service.Init() imooc.RegisterCapHandler(service.Server(),&CapServer{}) err := service.Run() if err != nil { panic(err) } } ``` </details> <br /> <details> <summary> proto/cap/imooc.proto </summary> ``` syntax ="proto3"; package go.micro.service.imooc; option go_package="./imooc"; service Cap{ rpc SayHello(SayRequest)returns(SayResponse){} } message SayRequest { string message = 1; } message SayResponse{ string answer=1; } ``` </details> <br /> <details> <summary>go.mod </summary> ``` module cap go 1.16 require ( github.com/asim/go-micro/v3 v3.5.0 github.com/golang/protobuf v1.4.2 google.golang.org/protobuf v1.23.0 ) ``` </details> <br /> 1. 编译 proto ``` cd proto/cap protoc --go_out=./ --micro_out=./ *.proto ``` 3. 运行 ``` > go run server.go > go run client.go answer:"我们的口号是hello world" ```