2014-06-05 1 views
1

파이프 라인 다중 HGETALL 명령을 관리했지만 문자열로 변환 할 수 없습니다.Redigo 파이프 라인 결과를 문자열로 변환

[[[116 105 116 108 101] [104 105]] [[116 105 116 108 101] [104 101 108 108 111]]] 

그러나 :

// Initialize Redis (Redigo) client on port 6379 
// and default address 127.0.0.1/localhost 
client, err := redis.Dial("tcp", ":6379") 
    if err != nil { 
    panic(err) 
} 
defer client.Close() 

// Initialize Pipeline 
client.Send("MULTI") 

// Send writes the command to the connection's output buffer 
client.Send("HGETALL", "post:1") // Where "post:1" contains " title 'hi' " 

client.Send("HGETALL", "post:2") // Where "post:1" contains " title 'hello' " 

// Execute the Pipeline 
pipe_prox, err := client.Do("EXEC") 

if err != nil { 
    panic(err) 
} 

log.Println(pipe_prox) 

그것은만큼 당신이 문자열이 아닌 결과를 보여주는 편안있는 한 괜찮은 .. 내가지고있어하는 것은 이것이다 :

내 샘플 코드는 이것이다 내가 필요한 것은 : 나뿐만 아니라 다음과 같은 다른 조합을 시도했습니다

"title" "hi" "title" "hello" 

:

result, _ := redis.Strings(pipe_prox, err) 

log.Println(pipe_prox) 

하지만 내가 가진 전부입니다 : []

내가 여러으로 HGET키 값 명령을 작동하는 것을 유의해야하지만 그게 내가 필요하지 않습니다.

내가 뭘 잘못하고 있니? "수치지도"를 문자열로 변환하려면 어떻게해야합니까? 어떤 도움

답변

4

HGETALL 반환이 문자열로 변환해야하는 값의 자신의 시리즈, 그리고 파이프 라인은 이러한 일련의 반환에 대한

감사합니다. redis.Values generic을 사용하여이 외부 구조를 먼저 분해 한 다음 내부 조각을 파싱 할 수 있습니다.

// Execute the Pipeline 
pipe_prox, err := redis.Values(client.Do("EXEC")) 

if err != nil { 
    panic(err) 
} 

for _, v := range pipe_prox { 
    s, err := redis.Strings(v, nil) 
    if err != nil { 
     fmt.Println("Not a bulk strings repsonse", err) 
    } 

    fmt.Println(s) 
} 

인쇄 :

[title hi] 
[title hello] 
0

당신이 이런 식으로 작업을 수행 할 수 있습니다

pipe_prox, err := redis.Values(client.Do("EXEC")) 
for _, v := range pipe_prox.([]interface{}) { 
    fmt.Println(v) 
}