V
V
v- death2015-08-14 13:01:33
go
v- death, 2015-08-14 13:01:33

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))
}

On the screen I get hello, rty546!
%!(EXTRA string=Familu !
, string=ty4567)
String = passed parameter. Thanks to all

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
v- deathi, 2015-08-14
@vGrabko99

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))
}

but how to do it in 1 line?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question