//main.go
func (self *GoodsController) GoodsEditGet(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
tmpID, ok := vars["id"]
sess := session.Instance(w, r)
if !ok {
tpl.Error("Invalid goods id")
}
id, _ := strconv.ParseInt(tmpID, 10, 64)
goods, err := service.NewGoodsService(w, r).GetGoodsDetail(id)
if err != nil {
//This utility function will not stop the rest of the code being executed
util.RedirectWithMessage(w, r, err.Error(), "/system/inventory/goods")
}
//This line will be executed even though the above line producing error
goodsUom, err := service.NewGoodsService(w, r).GetGoodsUom(id)
if err != nil {
util.RedirectWithMessage(w, r, err.Error(), "/system/inventory/goods")
}
}
//package utility
func RedirectWithMessage(w http.ResponseWriter, r *http.Request, errMsg string, redirect string) {
sess := session.Instance(w, r)
sess.FlashError(errMsg)
sess.FlashForm(r)
sess.Save(r, w)
http.Redirect(w, r, redirect, http.StatusFound)
return
}
May i know how to stop execution rest of the code after called to function RedirectWithMessage ?
Putting return at the end of that function didn't rest of the code being executed
Im looking the equivalent of Php version of redirect in Golang:
fuunction foo($location){
header(“Location : $location”);
exit();
}
foo("/bar"):
echo "blah"; //this will not be executed
Edited cause of overzealous down-vote.
Yes im fully aware that i can put return statement after a called to RedirectWithMessage. I just dont want to clutter my code with return statement all the place.Instead i just put once inside the function.
I just wonder is there any better solution to that? Can i achieve the same behaviour like php code i show?