Answer the question
In order to leave comments, you need to log in
Golang and how to create a global variable?
Given a minimal http server, the listener(net.Listener) is started in the main package. Now I want to make it so that when a certain url is called, the server stops, i.e. called listener.stop()
Okay, let's do http.HandleFunc. The handler (located in another package) contains 2 variables - the responder and the request, no references to main. Question - How to reach the listener in the main-package?
The question is not about how to dodge in this case. It is clear that you can place the handler in the same package and calmly pull the stop function, you can probably take the listener out of the main into a separate package, which can be pulled from the handler package and again reach the stop method.
The question actually boils down to how to pass a "command" (function call) to the main package from other packages?
Answer the question
In order to leave comments, you need to log in
Create a channel in the main function and pass it to the handler when creating the latter.
You will be able to read from the pipe to main and write to it from the handler package and vice versa.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question