Given that you have two instances of http.ServeMux,
and you wish for them to be served at the same port number, like so:
muxA, muxB http.ServeMux
//initialise muxA
//initialise muxB
combinedMux := combineMux([muxA, muxB])
http.ListenAndServe(":8080", combinedMux)
How would one go about writing the combinedMux function, as described above?
... or is there an alternative way to accomplish the same thing?
The SeverMux type is itself a http.Handler, therefore you can nest them easily. For example:
I'm not quite sure what you mean exactly with "combining" them. If you want to try one handler first and then another one in case of 404, you might do it like this (untested):
This has obviously some disadvantages like storing the whole response in memory. Alternatively, if you don't mind calling your handler twice, you can also set
rec.Body = nil, check justrec.Codeand callmuxA.ServeHTTP(w, r)again in case of success. But it's probably better to restructure your application so that the first approach (nested ServerMux) is sufficient.