Oh, yeah, that works. Thanks Aldrin.
The reason that I'm asking is that the "solution" that I found on the web
isn't working for me:
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)}
On Monday, June 3, 2019 at 11:55:46 PM UTC-4, Aldrin Leal wrote:
>
> 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)
> }
>
> --
> -- Aldrin Leal, <[email protected] <javascript:>> /
> https://ingenieux.io/about/
>
>
> On Mon, Jun 3, 2019 at 10:48 PM Tong Sun <[email protected] <javascript:>>
> 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 [email protected] <javascript:>.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/golang-nuts/1bc9f505-9638-49e2-b873-6f4d7b88dfbc%40googlegroups.com
>>
>> <https://groups.google.com/d/msgid/golang-nuts/1bc9f505-9638-49e2-b873-6f4d7b88dfbc%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
--
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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/golang-nuts/90caaf41-cec3-4ba8-9d07-bf9080923cfa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.