I was reading lot about Go and see that I cannot have optional parameters (nor default value for some parameters) in methods or functions. Lets take for example simple CRUD.
I want to use same controller action for create and update and just to do simple:
func (c Account) User(user models.User, verifyPassword string, id ...int64) {
/*
validate user etc
*/
if len(id) > 0 {
c.Txn.Update(&model)
} else{
c.Txn.Insert(&model)
}
}
When I try to do {{ url "Account.User" }}
in a template,
I get an error saying that I am missing parameters.
Once I hardcode value for route, I have some invalid memory address or nil pointer dereference on an unknown part of code.
Without optional/default parameters I will have code redundancy for CRUD that I do not want!
So instead of one function I will have to use two functions for the same thing that are only different in one line of code.
Since I started learning Go and Revel seven days ago, maybe I've missed something. The only reason I used Go and Revel is for speed. I am using this for some project that has a lot of requests (millions per day).
Is there any way to solve this?
Update
First problem is on the view: {{ url "Account.User" }}
When I used it like this, it says:
missing parameter id for route Account.User in func (c Account) User(user models.User, verifyPassword string, id ...int64)
So this is more of a Revel problem then Go.
Or you can try variadic arguments parsing, like how Python uses *args