M
M
Multigame2017-07-01 23:15:46
go
Multigame, 2017-07-01 23:15:46

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

2 answer(s)
A
Alexander Pavlyuk, 2017-07-01
@pav5000

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.

R
rustler2000, 2017-07-01
@rustler2000

Add a Context to the request and pass Server in it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question