Skip to Content
Building RESTful Web services with Go
book

Building RESTful Web services with Go

by Naren Yellavula
December 2017
Intermediate to advanced
316 pages
6h 58m
English
Packt Publishing
Content preview from Building RESTful Web services with Go

Query-based matching

Query parameters are those that get passed along with the URL. This is what we commonly see in a REST GET request. Gorilla Mux can create a route for matching a URL with the given query parameters:

http://localhost:8000/articles/?id=123&category=books

Let us add functionality to our program:

// Add this in your main programr := mux.NewRouter()r.HandleFunc("/articles", QueryHandler)r.Queries("id", "category")

It limits the query with the preceding URL. The id and category match with the Queries list. Empty values are allowed for parameters. QueryHandler looks like this. You can use request.URL.Query() to obtain query parameters in your handler function:

func QueryHandler(w http.ResponseWriter, r *http.Request){ queryParams ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Hands-On RESTful Web Services with Go - Second Edition

Hands-On RESTful Web Services with Go - Second Edition

Naren Yellavula
Microservices with Go

Microservices with Go

Alexander Shuiskov

Publisher Resources

ISBN: 9781788294287Supplemental Content