1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
| package main
import ( "fmt"
"github.com/garyburd/redigo/redis" )
func main() { c, err := redis.Dial("tcp", "127.0.0.1:6379") if err != nil { fmt.Println("Connect to redis error", err) return } defer c.Close()
_, err = c.Do("SET", "username", "nick") if err != nil { fmt.Println("redis set failed:", err) }
username, err := redis.String(c.Do("GET", "username")) if err != nil { fmt.Println("redis get failed:", err) } else { fmt.Printf("Get username %v \n", username) }
_, err = c.Do("lpush", "mylist", "ofo", "mobike", "foo") if err != nil { fmt.Println("redis lpush failed", err.Error()) }
_, err = c.Do("rpush", "mylist", "bluegogo", "xiaolan", "xiaoming") if err != nil { fmt.Println("redis rpush failed", err.Error()) }
num, err := c.Do("llen", "mylist") if err != nil { fmt.Println("mylist get len err", err.Error()) } else { fmt.Println("mylist's len is ", num) }
values, err := redis.Values(c.Do("lrange", "mylist", 0, 10)) if err != nil { fmt.Println("lrange err", err.Error()) } fmt.Printf("mylist is:") for _, v := range values { fmt.Printf(" %s ", v.([]byte)) } fmt.Println()
_, err = c.Do("ltrim", "mylist", 0, 4) if err != nil { fmt.Println("ltrim error", err.Error()) } else { values, err = redis.Values(c.Do("lrange", "mylist", 0, 4)) if err != nil { fmt.Println("ltrim failed:", err.Error()) } fmt.Printf("ltrim mylist is:") for _, v := range values { fmt.Printf("%s ", v.([]byte)) } fmt.Printf("\n") }
val, err := c.Do("lindex", "mylist", 2) if err != nil { fmt.Println("lindex error:", err.Error()) } else { fmt.Printf("lindex get result :%s\n", val) }
_, err = c.Do("rpoplpush", "mylist", "mybike") if err != nil { fmt.Println("rpoplpush failed:", err.Error()) } else { values, err = redis.Values(c.Do("lrange", "mylist", 0, 10)) if err != nil { fmt.Println("lrange failed:", err.Error()) } for _, v := range values { fmt.Printf("rpoplpush %s\n", v.([]byte)) }
values, err = redis.Values(c.Do("lrange", "mybike", 0, 10)) if err != nil { fmt.Println("lrange failed:", err.Error()) } for _, v := range values { fmt.Printf("rpoplpush %s\n", v.([]byte)) } }
_, err = c.Do("lset", "mylist", 2, "mysql") if err != nil { fmt.Println("lset error:", err.Error()) } val, err = c.Do("lindex", "mylist", 2) if err != nil { fmt.Println("lset error:", err.Error()) } else { fmt.Printf("lset get result:%s\n", val) }
_, err = c.Do("lrem", "mylist", 1, "mysql") if err != nil { fmt.Println("lrem error", err.Error()) } else { values, err = redis.Values(c.Do("lrange", "mylist", 0, 10)) if err != nil { fmt.Println("ltrim failed:", err.Error()) } for _, v := range values { fmt.Printf("lrem mylist: %s", v.([]byte)) } fmt.Printf("\n") }
_, err = c.Do("lpop", "mylist") if err != nil { fmt.Println("lpop failed:", err.Error()) } else { values, err = redis.Values(c.Do("lrange", "mylist", 0, 10)) if err != nil { fmt.Println("lpop failed:", err.Error()) } fmt.Printf("lpop mylist :") for _, v := range values { fmt.Printf("lpop mylist %s", v.([]byte)) } fmt.Printf("\n") }
_, err = c.Do("rpop", "mylist") if err != nil { fmt.Println("rpop failed", err.Error()) } else { values, err = redis.Values(c.Do("lrange", "mylist", 0, 10)) if err != nil { fmt.Println("rpop failed:", err.Error()) } fmt.Printf("rpop mylist :") for _, v := range values { fmt.Printf("lpop mylist %s", v.([]byte)) } fmt.Printf("\n") }
res, err := c.Do("blpop", "mylist", 10) if err != nil { fmt.Println("blpop error") } else { fmt.Printf("blpop from mylist get:%s\n", res) }
res, err = c.Do("blpop", "tmpbike", 10) if err != nil { fmt.Println("blpop time out") } else { fmt.Println("blpop from tmpbike get:", res) }
res, err = c.Do("brpop", "tmpbike", 10) if err != nil { fmt.Println("brpop error") } else { fmt.Printf("brpop from tmpbike get :%s\n", res) }
res, err = c.Do("brpop", "mybike", 10) if err != nil { fmt.Println("brpop time out") } else { fmt.Printf("brpop from mybike get:%s ", res)
}
}
|