I am new to golang, I am trying to create a web project with the julienschmidt/httprouter. I am searching to create a well formatted and well structured project so I have two questions about performances passing and returning value or pointers.
In my case I want to create a function that from the request returns an object so I have created it:
// StoreController
func (storeController *StoreController) New(w http.ResponseWriter, r *http.Request) {
store, err := utilities.GetStoreFromRequest(r)
// other stuff
return
}
// Utilities package
func GetStoreFromRequest(r *http.Request) (*models.Store, error) {
store := models.Store{}
err := json.NewDecoder(r.Body).Decode(&store)
// return a pointer is better than returning an object?
return &store, err
}
Is it right or it is better to create a store object in the storeController and pass it to the function like:
// StoreController
func (storeController *StoreController) New(w http.ResponseWriter, r *http.Request) {
store := models.Store{}
err := utilities.GetStoreFromRequest(r, &store)
// other stuff
return
}
// Utilities package
func GetStoreFromRequest(r *http.Request, store *models.Store) error {
err := json.NewDecoder(r.Body).Decode(store)
return err
}
The other question is about the pointer, is too paranoid to pass and return always pointers instead of object and errors or not? Thanks