Go: "Static" method design
I'm looking for advice on the best way to clean-up the following
structure. I know Go doesn't have static methods and it's usually better
to encapsulate functionality in a separate package. My struct types
reference each other, and so cannot be declared in separate packages
because of circular imports.
type Payment struct {
User *User
}
type User struct {
Payments *[]Payments
}
func (u *User) Get(id int) *User {
// Returns the user with the given id
}
func (p *Payment) Get(id int) *Payment {
// Returns the payment with the given id
}
But, if I want to load a user or payment, I have to initialize the struct
and then throw it away:
u := User{}
user := u.Get(585)
I could namespace the functions themselves, which strikes me as unclean:
func GetUser(id int) *User {
// Returns the user with the given id
}
func GetPayment(id int) *Payment {
// Returns the payment with the given id
}
I would really like to be able to just call .Get or similar on the struct
without writing the name of the struct in the function itself. Is there
any way to accomplish that?
No comments:
Post a Comment