Sunday, November 16, 2014

Routing for a Go Web Project

From a python programmer's perspective, these are lessons I've learned from developing a web app using Go.

After I've planned my project structure for my go app, I started to look into how I should go about developing the app itself. I read the detailed tutorial from the golang wiki wiki site. Although it seemed simple enough to build a web app, I immediately recognized some things I would miss from the python/django world. For example, I didn't want to have code in my view to parse and validate the URLs. I started to look around for a solution and came across Gorilla Toolkit, specifically the mux package. It provided functionality that I was more accustomed to when I was using Django.
In the case of capturing values from the URL or applying a regex to the URL, you would specify routes as:
r := mux.NewRouter()
r.HandleFunc("/products/{key}", ProductHandler)
r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
In the ArticleHandler function, you can retreive the value of "category" or "id" by:
vars := mux.Vars(request)
category := vars["category"]
id := vars["id"]
You can also restrict access to a view handler by specifying the request to use a specific method:
r.Methods("GET", "POST")
or requiring the request to use specific headers:
r.Headers("X-Requested-With", "XMLHttpRequest")
You can view the mux documentation all the functionality available. In general, this package helps remove a lot of boilerplate code from the view functions and makes a Django developer feel more at home when building a go web app.


  1. It's very nice what you're sharing with your audience. I have read your article and I think it's very nice. You can apply for an India business visa by clicking the link we just provided. Thanks for adding value to the post.

  2. Hello everyone, Are you traveling to India, then you need a visa. You can apply for your India e visa online. At any time of the day, this can be done easily and quickly on an online website. You won't need to visit the embassy or the consulate. All information about the visa. India can be found on this Indian e-visa website. If you want, you can also print a summary, start your application by opening the online application form in it.

  3. Hello, I wanted to write a little Info related to Visa. Are you interested in traveling to any country? Yes, you can e visa apply online. You can fill out your visa application form online within 5 to 10 minutes via our Visacent website. We offer visas to citizens of over 190 countries. You can read more info about visas via our website.

  4. NordVPN 7.7.3 Crack for PC is a basic and well-known software that allows users to easily surf the Internet anonymously and securely.! Nord VPN Cracked pc

  5. Kaspersky Total Security 2022 Crack array of Windows antivirus products continue the company’s physically powerful custom of top-notch defense with a modest scheme impact.Kaspersky Crack

  6. Merry Christmas Wishes for Loved Ones · Spending time with you is what I wish for us this season. Christmas MSG To Loved Ones

  7. Fl Studio Crack includes a graphical user interface based on a pattern-based music sequencer. It supports multiple MIDI inputs and outputs and can be used with a variety of MIDI controllers. It also includes a wide range of built-in virtual instruments, synthesizers, samplers, and effects plugins. Users can also add their own VST and VSTi plugins for additional functionality.