[Golang] Get Query String of URL in net/http Handler


Get query string of URL in request handler of HTTP server via Go standard net/http Package.

Question:

Assume the URL of the HTTP request is

https://example.com/?name=john

I want to get the value of name, i.e., john from the URL. How to get it in HTTP request handler of net/http?

Answer:

import (
      "net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
      name := r.URL.Query().Get("name")
      // the value of name is john
}

For more information about how to access the query string, see net/url package.


Tested on:

  • Ubuntu Linux 16.10
  • Go 1.8

References:

[1]
[2][Golang] JSONP Server Implementation Code
[3]How to make a secured user authentication and authorization system? What are the things I should look for? : golang
[4]Structure of API project, i.e.: Where should what part of the code be? : golang
[5]GoBuffalo and Authboss (authentication system) integration : golang
[6]Create Self-Signed Certs and Pin them with Go : golang