GoLang Service Design
Service Design
You can refer to the following image.

Data Flow
- User request goes to REST endpoint
- Validate user input at handler/controller level. Validation does not cover checking external system (db, redis, http). Pure just checking based on the payload request.
- Build request for service object (if necessary), then call service function.
- Based on the response from the service object, the handler/controller determines:
- If successful, build & return ok response / payload to user.
- If it fails, the error returns immediately.
Service method naming guide
Use only [Create, Get, Put, Delete, Update, List] prefix. e.x. :
Create
to create a new resource (whether writing to storage or not)- example:
CreateUser(ctx context.Context, ID string) (User, error)
- example:
Get
to get single resource which pre-existing in datasource- example:
GetUser(ctx context.Context, ID string) (User, error)
- example:
Update
to update a pre-existing single resource directly overall (replace existing resource)- example:
UpdateUser(ctx context.Context, u *User)eerror
- example:
Put
to partially update single resource.List
to get many resources that already exist in datasourceListUser(ctx context.Context, options FilterOptions) ([]User, error)
Delete
to delete single resource in datasourceDeleteUser(ctx context.Context, ID string) error
- Use
context.Context
as the first parameter for functions that interact with external systems. Document reference using context parameter- External call to redis? use
context.Context
- query to postgres / mongodb ? use
context.Context
- External call to redis? use