8

While developing a REST api in Go, how can we use path params? meaning to say what will be the format of the URI?

http://localhost:8765/myapp/{param1}/entries/{param2}

I tried using something like this to create the route but the handler function is not getting invoked.

Please note that, i intent to use only the net/http package , not any other web framework like gorilla mux.

1

4 Answers 4

3

Go 1.22 can now handle path values. See the Go blog article routing-enhancements. If you are listening on http://localhost:8765 as in your example, then you would use a handler with placeholders. You can also handle a specific http method. For instance, you can limit the handler to the GET method:

http.HandleFunc("GET /myapp/{param1}/entries/{param2}", handleEntries)

func handleEntries(w http.ResponseWriter, r *http.Request) {
    param1 := r.PathValue("param1")
    param2 := r.PathValue("param2")

    // do stuff with parameters
}

See the go docs for PathValue and ServeMux.

Sign up to request clarification or add additional context in comments.

Comments

2

What I tend to do is nested handlers. "/" is handled by the root handler. It pops the first part of the path, assigns the rest back to req.URL.Path (effectively acting like StripPrefix), determines which handler handles routes by that prefix (if any), then chains the appropriate handler. If that handler needs to parse an ID out of the path, it can, by the same mechansim - pop the first part of the path, parse it as necessary, then act.

This not only has no external dependencies, but it is faster than any router could ever be, because the routing is hard-coded rather than dynamic. Unless routing changes at runtime (which would be pretty unusual), there is no need for routing to be handled dynamically.

Comments

1

Well this is why people use frameworks like gin-gonic because this is not easy to do this in the net/http package IIUC.

Otherwise, you would need to strings.Split(r.URL.Path, "/") and work from those elements.

2 Comments

Ya, i had this down with gorilla Mux. But in std lib, the net/http package couldn't allow me such flexibility. I had to do with string trimming and splitting of the url path as suggested above by you. Just wanted to confirm if there is indeed any other way to get this done with anything from the std lib. I am new to GO, pardon my ignorance. The other thing i couldn't get was to have handlers based on methods.Need to access the mothod as well from the request object it seems.
Don't worry. All golang Web developers have had the same thoughts. ;)
0

With net/http the following would trigger when calling localhost:8080/myapp/foo/entries/bar

http.HandleFunc("/myapp/", yourHandlerFunction)

Then inside yourHandlerFunction, manually parse r.URL.Path to find foo and bar.

Note that if you don't add a trailing / it won't work. The following would only trigger when calling localhost:8080/myapp:

http.HandleFunc("/myapp", yourHandlerFunction)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.