Backend Development (Go)
Rules
Conventions for models, services, requests, responses, and routes in the Go backend.
Model
- Model's file name should be written in singular snake-cased words. For example:
user.go,user_role.go. - Model's table name should be defined in the model file. For example:
type User struct {
Id int64 `gorm:"id,primaryKey"`
Name string `gorm:"name"`
}
func (User) TableName() string {
return "users"
}Kafka Service
- Service file should be inside the module directory. For example if the service handle process related to user module, then the service file should be inside the
kafka_service/userdirectory. - Service file name should be written in singular snake-cased words. For example:
user.go. - This directory only contains functions that only can be called from kafka listener.
- It's not allowed to retrieve data directly from the service, you should use repository instead.
Mock
-Still working on it-Internal & pkg
These directories contains wrapper functions and the logic before the process enter the service.
If the scripts inside the internal/ or pkg/ folder needs an update, you should confirm to your tech-leader because it may affect service or repository on the project.
Request
JSON
-Still working on it-Form (multipart/form-data)
-Still working on it-Response
JSON
Success
When you send success API response, you must use Response function wrapper from pkg/util/api package. Explanation:
- The first parameter is the gin context pointer.
- The second parameter is the data (any).
Error
When you send error API response, you must use ResponseErr function wrapper from pkg/util/api package. Explanation:
- The first parameter is the gin context pointer.
- The second parameter is the response code defined in
pkg/util/response/response.go. - The third parameter is string defined in
pkg/util/response. - The last parameter is the data in any type.
Route
- Route path & subpath format is descriptive kebab-cased words. For example:
/form/export-to-pdf. - Route path is named after the related module. For example if the related module is
userthen the first path is/userfollowed by the subpath (if there is any).