Answer the question
In order to leave comments, you need to log in
How to handle multiple parameters?
Hello. I use the router https://github.com/julienschmidt/httprouter
While I am passing 1 parameter, everything is buzzing. But as I tried to transfer two, it didn’t work out what I wanted.
Here is the code
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
"log"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"),"Familu", ps.ByName("fam"))
}
func main() {
router := httprouter.New()
router.GET("/", Index)
router.GET("/hello/:name/:fam", Hello)
log.Fatal(http.ListenAndServe(":9001", router))
}
Answer the question
In order to leave comments, you need to log in
Works if the output is done separately for each parameter
package main
import (
"fmt"
"github.com/julienschmidt/httprouter"
"net/http"
"log"
)
func Index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
fmt.Fprint(w, "Welcome!\n")
}
func Hello(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
fmt.Fprintf(w, "hello, %s!\n", ps.ByName("name"))
fmt.Fprintf(w, "Fam, %s!\n", ps.ByName("fam"))
}
func main() {
router := httprouter.New()
router.GET("/", Index)
router.GET("/hello/:name/:fam", Hello)
log.Fatal(http.ListenAndServe(":9001", router))
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question