This document is inherit from and complement to https://github.com/unknwon/go-code-convention, which only different content will be described.
2.1 Options should always be pass by value, not pointer to avoid accidental changes to the original copy in the caller.
func MyFunc(opts MyOptions) { ... }
2.2 When initialize struct of options, order of fields should be same as how they are defined.
type MyOptions struct{
ID int64
Name string
Email string
}
opts := MyOptions{
ID: 123,
Name: "joe",
Email: "fake@local",
}
3.1 modules/models
3.1.1 Private function should only perform target operation without creating corresponding user action.
newIssue
should only create new issue and related objects; create new issue action is not its job.3.1.2 Insert the an object can obtain its ID in database to the origin variable of struct.
issue :=&Issue{} // issue.ID is 0
_, err := x.Insert(issue)
// issue.ID now has valid value, e.g. 85
// use this ID for the subsequent operations.
3.1.3 No database query in (type).AfterSet
method, only pure calculations. Put all database queries in corresponding (type).loadAttributes
method.