Go/Golang Read Query String

package main

import (
"fmt"
"log"
"net/http"
"github.com/gorilla/mux"
)

func YourHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
log.Println("one=" + vars["one"])
log.Println("two=" + vars["two"])
query := r.URL.Query()
log.Println(query["three"])
log.Println(query["four"])
log.Println("four[0]=" + query["four"][0])
log.Println("four[1]=" + query["four"][1])
log.Println(query["five"])
fmt.Fprintf(w,"Gorilla!\n")
}

func main() {
r := mux.NewRouter()
r.HandleFunc("/{one}/{two}", YourHandler)
log.Fatal(http.ListenAndServe(":8000", r))
}
http://localhost:8000/1/2?three=3&four=9&four=8
curl 'http://localhost:8000/1/2?three=3&four=9&four=8'
r.HandleFunc("/{one}/{two}", YourHandler)

--

--

--

A software engineer, a rock climbing, inline skating enthusiast, a husband, a father.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

A Clone Army of the Jenkins Republic

Format Dates in Flutter

MetaTube Airdrop Metaverse Game

An Interview with Flexbox

Authenticate Next.js SPA with ASP.NET 6 Identity and Duende Identity Server Part 1

Some thoughts on iterative numerical algorithms for integer factoring, from a crank

The Three Layered Architecture

Best Security Practices For Azure

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kevin FOO

Kevin FOO

A software engineer, a rock climbing, inline skating enthusiast, a husband, a father.

More from Medium

Use pprof to view go program stack traces

Golang developer — Part 2 — Intermediate of Golang

Battle of Concurrency | Goroutines vs Threads.

Test Driven Development in GO