import ( "github.com/emicklei/go-restful" "net/http" ) func myHandler(request *restful.Request, response *restful.Response) { // some logic if err != nil { response.WriteError(http.StatusInternalServerError, err) return } // more logic }
import ( "github.com/emicklei/go-restful" ) func (s *MyService) Register(container *restful.Container) { ws := new(restful.WebService) ws. Path("/users"). Route(ws.GET("/{user-id}").To(s.getUser)) container.Add(ws) } func (s *MyService) getUser(request *restful.Request, response *restful.Response) { userID := request.PathParameter("user-id") if err := s.validateUserID(userID); err != nil { response.WriteErrorString(http.StatusBadRequest, err.Error()) return } // more logic }In this example, the `getUser` function is called when a GET request is made to `/users/{user-id}`. If there is an error validating the user ID, the response is written with a 400 status code and the error message. Both examples use the `go-restful` package to write error responses.