RPC Directory Structure

[!TIP] This document is machine-translated by Google. If you find grammatical and semantic errors, and the document description is not clear, please PR

  1. .
  2. ├── etc // yaml configuration file
  3. └── greet.yaml
  4. ├── go.mod
  5. ├── greet // pb.go folder①
  6. └── greet.pb.go
  7. ├── greet.go // main entry
  8. ├── greet.proto // proto source file
  9. ├── greetclient // call logic ②
  10. └── greet.go
  11. └── internal
  12. ├── config // yaml configuration corresponding entity
  13. └── config.go
  14. ├── logic // Business code
  15. └── pinglogic.go
  16. ├── server // rpc server
  17. └── greetserver.go
  18. └── svc // Dependent resources
  19. └── servicecontext.go

[!TIP] ① The name of the pb folder (the old version folder is fixed as pb) is taken from the value of option go_package in the proto file. The last level is converted according to a certain format. If there is no such declaration, it is taken from the value of package. The approximate code is as follows:

  1. if option.Name == "go_package" {
  2. ret.GoPackage = option.Constant.Source
  3. }
  4. ...
  5. if len(ret.GoPackage) == 0 {
  6. ret.GoPackage = ret.Package.Name
  7. }
  8. ret.PbPackage = GoSanitized(filepath.Base(ret.GoPackage))
  9. ...

[!TIP] For GoSanitized method, please refer to google.golang.org/protobuf@v1.25.0/internal/strs/strings.go:71

[!TIP] ② The name of the call layer folder is taken from the name of the service in the proto. If the name of the sercice is equal to the name of the pb folder, the client will be added after service to distinguish between pb and call.

  1. if strings.ToLower(proto.Service.Name) == strings.ToLower(proto.GoPackage) {
  2. callDir = filepath.Join(ctx.WorkDir, strings.ToLower(stringx.From(proto.Service.Name+"_client").ToCamel()))
  3. }