I
I
impressive172021-11-01 21:22:09
go
impressive17, 2021-11-01 21:22:09

How to setup subroute in gorilla mux golang?

Let's say I have a site example.com and a subdomain sub.example.com
I want to handle example.com/ and sub.example.com/ with different handlers
I do it like this

router := mux.NewRouter()
r1 := router.Host("example.com/").Subrouter()
r1.HandleFunc("/", hand1)

r2 := router.Host("sub.example.com").Subrouter()
r2.HandleFunc("/", hand2)

http.ListenAndServe(":8080", router)


But it doesn't work, what's wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-11-01
@EvgenyMamonov

You need to remove '/' from example.com/, i.e. should be router.Host("example.com").Subrouter()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question