Gorilla/Mux Reverse mapping URL

41 views
Skip to first unread message

Farhad Kocharli

unread,
Jun 13, 2020, 6:34:20 AM6/13/20
to Gorilla web toolkit

I’m new in development. Now trying to learn gorilla/mux router. The question is about reversed urls. In gorilla/mux I know we name them with .Name() method and access with .Url(). Could someone explain real use case of reserved URL’s(Reverse mapping URL)? But a few hours of googling didn’t help me to find any info about that why we even need them? Will be really thankfull if you could show some practical examples.


Matt S

unread,
Jun 13, 2020, 10:23:32 AM6/13/20
to goril...@googlegroups.com
Could you expand on this? Not sure what you mean?



On Sat, Jun 13, 2020 at 3:34 AM Farhad Kocharli <kocharl...@gmail.com> wrote:

I’m new in development. Now trying to learn gorilla/mux router. The question is about reversed urls. In gorilla/mux I know we name them with .Name() method and access with .Url(). Could someone explain real use case of reserved URL’s(Reverse mapping URL)? But a few hours of googling didn’t help me to find any info about that why we even need them? Will be really thankfull if you could show some practical examples.


--
You received this message because you are subscribed to the Google Groups "Gorilla web toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gorilla-web...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gorilla-web/fa89852e-b293-419c-bc3d-fe177dc05ed1o%40googlegroups.com.

Farhad Kocharli

unread,
Jun 13, 2020, 11:14:31 AM6/13/20
to Gorilla web toolkit
well. let me explain. below section is from mux documentation, could someone explain what is this? why we need this? and in which real example we use this?

Now let's see how to build registered URLs.

Routes can be named. All routes that define a name can have their URLs built, or "reversed". We define a name calling Name() on a route. For example:

r := mux.NewRouter()
r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
  Name("article")

To build a URL, get the route and call the URL() method, passing a sequence of key/value pairs for the route variables. For the previous route, we would do:

url, err := r.Get("article").URL("category", "technology", "id", "42")

...and the result will be a url.URL with the following path:

"/articles/technology/42"

On Saturday, June 13, 2020 at 6:23:32 PM UTC+4, Matt S wrote:
Could you expand on this? Not sure what you mean?


On Sat, Jun 13, 2020 at 3:34 AM Farhad Kocharli <kocharl...@gmail.com> wrote:

I’m new in development. Now trying to learn gorilla/mux router. The question is about reversed urls. In gorilla/mux I know we name them with .Name() method and access with .Url(). Could someone explain real use case of reserved URL’s(Reverse mapping URL)? But a few hours of googling didn’t help me to find any info about that why we even need them? Will be really thankfull if you could show some practical examples.


--
You received this message because you are subscribed to the Google Groups "Gorilla web toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to goril...@googlegroups.com.

Matt S

unread,
Jun 13, 2020, 11:59:08 AM6/13/20
to goril...@googlegroups.com
You would use this when:

• Constructing redirects to send to a client - eg programmatically, so you don’t have to fix the URLs in your code elsewhere

• Building examples & tests 

• Generating docs 

 

To unsubscribe from this group and stop receiving emails from it, send an email to gorilla-web...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gorilla-web/592185e3-812d-414f-a2f6-8ea5b7379590o%40googlegroups.com.

Farhad Kocharli

unread,
Jun 13, 2020, 12:09:06 PM6/13/20
to Gorilla web toolkit
Thanks a lot. I though that this is something easy but couldn't figure out in my head how to use it. Now, thanks to you I have clear picture of usage.  Thanks again, I really appreciate your efforts to help.
Reply all
Reply to author
Forward
0 new messages