I would like to hear some advice from Golang gurus. Imagine I have a package for database layer with many table which I should define structs and functions for them. As an example, I have a table called "MyObject" and define a struct for it.
type MyObject struct {
RecID int
Name string
}
Is it convenient for functions related to this table/struct I define receiver like this:
func (o MyObject) AllMyObjects() ([]MyObject, error) {
// retreive all MyObjects by query
}
func (o *MyObject) OneMyObjects() (MyObject, error) {
// retreive one MyObject by query
}
In this way as my db package get bigger by structs and functions, it will look cleaner and easier to call methods like this (although I should define 1 extra line):
var myobject MyObject
var myobjects,err = myobject.AllMyObjects()
rather than calling functions directly on package level (db is name of my package)
var myobjects,err = db.AllMyObjects()
Is this Go way of organizing code or do you have other advice for this?