我知道有關於這個問題的一些問題和文章/文章,但是從我的新手視圖來看,並不完全。 事情是,我有一個主程序監聽一個端口並將調用重定向到一個特定的處理程序。典型結構:在Golang中使用Http處理程序的全局變量
func main() {
http.HandleFunc("/something", specificHandler)
http.ListenAndServe(":8080", nil)
}
隨着處理程序是這樣的:
func specificHandler(w http.ResponseWriter, r *http.Request) {
somepackage.foo()
}
然後somepackage,其中包含的功能富,有一些全局變量,主要是因爲他們需要函數共享(例如,當使用用容器/堆實現的優先級隊列時,該優先級隊列將從距離的全局矩陣中獲得交換函數中的優先級,當然這是可改變的)。還有很多其他的例子。總之,全局變量...
問題是,如你所見,這些變量是在處理程序的所有調用中共享的。這很糟糕。
我該如何解決這個問題?必須有一個簡單的方法來做到這一點,我還沒有到,因爲它看起來像平常一樣......
在此先感謝。
編輯
使其更清晰。舉例來說,在我的A *包,我有以下全局變量:
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
然後,PriorityQueueAStar實現如下:
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]] < 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]
}
那麼問題,就是,怎麼辦我一直這樣做,而沒有將所有這些地圖作爲全局變量?如果它們是結構體的一部分,我如何從優先級隊列函數訪問結構體?
事實上,你認識到你的變量是mod在不同的通話之間進行通話表明這是設計。聽起來像你想要某種容器。你會想要設計一個結構來適應你的需求,並將這個結構傳遞給某些包中的函數調用。 –
是的,這聽起來不錯,或者爲整個包創建一個結構並讓它包含變量。但是堆函數中的Less(i,j int)函數呢?那些將需要訪問該結構,我不認爲有辦法將它傳遞給他們... – jcasado94
調查頻道[chan](https://golang.org/ref/spec#Channel_types)他們解決了很多*與共享內存相關的問題。 – miltonb