// Declare a new rest API instance api := rest.NewApi() // Define a route that accepts a PathParam named 'id' router, _ := rest.MakeRouter( rest.Get("/user/:id", GetUser), ) // Register the routes and start the API api.SetApp(router) log.Fatal(http.ListenAndServe(":8080", api.MakeHandler()))
func GetUser(w rest.ResponseWriter, r *rest.Request) { // Extract the 'id' path param from the request id := r.PathParam("id") // Look up the user with the given ID in the database user, err := db.GetUserById(id) if err != nil { rest.Error(w, err.Error(), http.StatusInternalServerError) return } // Return the user as JSON w.WriteJson(user) }In this example, the GetUser function extracts the 'id' PathParam from the request and uses it to look up the requested user in a database. If the user is found, it is returned as JSON. If an error occurs, an HTTP 500 error response is returned. The package library for this example is "github.com/ant0ine/go-json-rest/rest".