Golang code specifications business development specifications

  1. The project is developed using the congestion model of the domain-driven model. Business implementation is in the internal directory, and the business layers are server, service, biz, data, and config. The server layer is the service registration layer, registering the system's http, grpc services, etc.; the service layer is the interface layer, processing external requests, calling internal implementations, and then returning data. The biz layer is the business logic layer. Based on the input from the service layer and the business purpose, the data layer is called to obtain data for business processing. The data layer is used to interact with databases and third-party libraries. It does not process business logic and is only responsible for obtaining data.
  2. The service layer needs to convert the request data into a biz layer struct, then call the biz layer method and pass in the converted biz layer struct example. The input parameters of the biz layer method are basic data types or biz objects, and the output parameters are also basic data types or biz objects.
  3. The data returned by the data layer method needs to be converted into the struct defined by the biz layer.
  4. Data is designed using interface + implementation. The struct of the data layer needs to implement the interface defined in biz.go
  5. The project uses the idea of ​​DDD to divide the biz layer according to fields and the data layer according to entities. Each object in the data layer only processes the data of its own entity object. Try not to use multi-table connection queries and prioritize code readability. level increased.
  6. The interface is uniformly defined in the api directory using protocolbuff, and the corresponding golang code is generated by compiling the protoc file. The name of the proto interface uses verbs, the nouns in the api path use plurals, the structure of the interface parameters uses the first letter of the camel case, and the instance of the interface parameter uses The first letter is in lowercase camel case.
  7. Delete using tombstone.
  8. Business logic involving multiple tables or multiple read and write operations on one table requires the use of transactions to ensure ACID
  9. Dependency injection is implemented using wire
  10. Similar implementations in multiple methods should be abstracted into one method; multiple logical processes in one method should be encapsulated into methods, and the business processing within one method should be simple and intuitive.

Guess you like

Origin blog.csdn.net/qq_26356861/article/details/131290132