Here is a BAD example (using global variables):
var globalThing string
func specificHandler(w http.ResponseWriter, r *http.Request) {
w.Write(globalConfigThing)
}
func main() {
globalThing = "Hello world!"
http.HandleFunc("/something", specificHandler)
http.ListenAndServe(":8080", nil)
}--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1bc9f505-9638-49e2-b873-6f4d7b88dfbc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Here is a BETTER example (not using global variables):
type specificHandler struct {
Thing string
}
func (h *specificHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
w.Write(h.Thing)
}
func main() {
http.Handle("/something", &specificHandler{Thing: "Hello world!"})
http.ListenAndServe(":8080", nil)
}wrapping into a typedef?typedef MyHandler struct {GlobalThing string}func (h *MyHandler) handle(w http.ResponseWriter, r *http.Request) {}func main() {h := MyHandler{}http.HandleFunc("/", MyHandler.handle)}
On Mon, Jun 3, 2019 at 10:48 PM Tong Sun <sunto...@gmail.com> wrote:
--Here is a BAD example (using global variables):
var globalThing string func specificHandler(w http.ResponseWriter, r *http.Request) { w.Write(globalConfigThing) } func main() { globalThing = "Hello world!" http.HandleFunc("/something", specificHandler) http.ListenAndServe(":8080", nil) }How to avoid using global variables?thx
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golan...@googlegroups.com.