복붙노트

[REDIS] Redigo 레디 스 풀 정말 전역 변수로되어 있습니까?

REDIS

Redigo 레디 스 풀 정말 전역 변수로되어 있습니까?

예에서 여기 Redigo 문서는 풀의 레디 스 풀은 주요 FUNC에서 전역 변수로 설정되어 있습니다. 그 일을 할 수있는 정결 한 방법이 있나요? 당신은 정말 글로벌 varibales 좌우 또는 같은 일을 달성의 더 나은,보다 바람직한 방법이 사용되어야 하는가?

해결법

  1. ==============================

    1.유일한 다른 용액은 I는 "인터페이스 방법에 컨텍스트 전달"에, 예를 들어 보았다 :

    유일한 다른 용액은 I는 "인터페이스 방법에 컨텍스트 전달"에, 예를 들어 보았다 :

    귀하의 경우, 구조체는 수영장, 핸들러 함수를 포함한다.

    type appContext struct {
        pool Pool
    }
    
    type appHandler struct {
        *appContext
        h func(a *appContext, w http.ResponseWriter, r *http.Request) (int, error)
    }
    
    func (ah appHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
       ...
    }
    
    
    func main() {
        context := &appContext{
            pool:    ...,
            // any other data
        }
    }
    
  2. from https://stackoverflow.com/questions/24971669/is-redigo-redis-pool-really-supposed-to-be-a-global-variable by cc-by-sa and MIT license