[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