Get a URL parameter from a Request

Often it’s important to read off the parameters sent through from the request. If it’s a GET request these will lie within the url itself and can be read using the request URL property. It’s important to note that when getting the parameters in this way it will always return an array (or slice?).

package main

import (
    "log"
    "net/http"
)

func main() {
    http.HandleFunc("/", handler)
    http.ListenAndServe(":8080", nil)
}

func handler(w http.ResponseWriter, r *http.Request) {

    keys, ok := r.URL.Query()["key"]
    
    if !ok || len(keys) < 1 {
        log.Println("Url Param 'key' is missing")
        return
    }

    // Query()["key"] will return an array of items, 
    // we only want the single item.
    key := keys[0]

    log.Println("Url Param 'key' is: " + string(key))
}

Leave a Reply

Your email address will not be published. Required fields are marked *