3 ms·
I love Go, but it seems like just about every HTTP request router is designed around abuse of global state.
by russell_h 13y ago
I love Go, but it seems like just about every HTTP request router is designed around abuse of global state.
- mvzink 13y agoIt's not designed around global state. It's true that just about every HTTP router has this "feature", but all it's doing is allocating a "global" server object by default and adding the module-level helper functions. In other words, if you only want a single server object and don't need to configure it, or if you just want a quick hello world example, you don't have to allocate the server object yourself. Without those extra convenience methods, this would be hello world's main: s := web.NewServer() s.Get("/(.*)", hello) s.Run("0.0.0.0:9999") Same goes for net/http and gorilla. net/http calls it the "default" server, web.go calls it the "main" server. I blame the influence of Sinatra etc.—the examples (for all three libraries) don't make it clear that this is what's going on and don't encourage making your own server objects to minimize global state.
- russell_h 13y agoRight, "designed" is probably a misleading way to put it. I just wish, like you say, that libraries would stop supporting and encouraging use of global routers.
- angularly 13y agoI am new to Go, but have dived right in and rewriting our current PHP backend in Go. This global state abuse sounds quite important to understand. Unfortunately I do not have any clue what you are talking about. Could you point me in a direction of some examples of someone doing it the right way?