title: Usage of global variables with Http handlers in Golang
body: <p>I know there are some questions and posts/articles regarding this question, but from my newbie view, not exactly.
  The thing is, I've got a main program listening to a port and redirecting the calls to a specific handler. The typical structure:</p>
  
  <pre><code>func main() {
      http.HandleFunc("/something", specificHandler)
      http.ListenAndServe(":8080", nil)
  }
  </code></pre>
  
  <p>With the handler being something like:</p>
  
  <pre><code>func specificHandler(w http.ResponseWriter, r *http.Request) {
      somepackage.foo()
  }
  </code></pre>
  
  <p>Then <strong>somepackage</strong>, which contains the function <strong>foo</strong>, has some global variables, basically because they're needed for functions to share (e.g., when using a priority queue implemented with a container/heap, which will get the priorities in the Swap function from a global matrix of distances which is of course changable). And many other examples. In summary, global variables...</p>
  
  <p>The problem is, as you might see, that those variables are shared among all the calls to the handler. And that's bad.</p>
  
  <p>How can I actually solve this? There must be an easy to way to do it that I haven't got to yet, because it looks like something so usual...</p>
  
  <p>Thanks in advance.</p>
  
  <hr>
  
  <p>EDIT</p>
  
  <p>To make it clearer. For example, in my A* package, I've got the following global variables:</p>
  
  <pre><code>var openVerticesAS PriorityQueueAStar
  
  // which vertices are closed
  var closedVertices map[int]bool
  
  // which vertices are currently open
  var openVertices map[int]bool
  
  // cost from start to node
  var gScore map[int]float64
  
  // cost from start to end, passing by node i (g+h)
  var fScore map[int]float64
  </code></pre>
  
  <p>Then, <strong>PriorityQueueAStar</strong> is implemented as follows:</p>
  
  <pre><code>type PriorityQueueAStar []int // rel id
  
  func (pq PriorityQueueAStar) Len() int { return len(pq) }
  
  func (pq PriorityQueueAStar) Empty() bool { return len(pq) == 0 }
  
  func (pq PriorityQueueAStar) Less(i, j int) bool {
      return fScore[pq[i]] &lt; fScore[pq[j]]
  }
  
  func (pq PriorityQueueAStar) Swap(i, j int) {
      pq[i], pq[j] = pq[j], pq[i]
  }
  
  func (pq *PriorityQueueAStar) Push(x interface{}) {
      *pq = append(*pq, x.(int))
  }
  
  func (pq *PriorityQueueAStar) Pop() interface{} {
      old := *pq
      n := len(old)
      rel := old[n-1]
      *pq = old[0 : n-1]
      return rel
  }
  
  func (pq PriorityQueueAStar) Top() interface{} {
      return pq[0]
  }
  </code></pre>
  
  <p>The question then, is, how do I keep doing this without having all those maps as global variables? If they are part of the struct, how do I access the struct from the priority queue functions?</p>