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
| package main
import ( "fmt" "log"
"github.com/gomodule/redigo/redis" )
func main() { log.Println("测试redigo")
conn, err := redis.Dial("tcp", "192.168.2.99:6379") checkErr(err) defer conn.Close()
addResult, err := conn.Do("SET", "mykey", "content") checkErr(err) log.Println(addResult)
addExResult, err := conn.Do("SET", "mykeyex", "content", "EX", 10) checkErr(err) log.Println(addExResult)
exRows, err := conn.Do("EXPIRE", "mykeyex", 5) checkErr(err) log.Println(exRows)
has, err := redis.Bool(conn.Do("EXISTS", "mykey")) checkErr(err) log.Println(has)
bytes, err := conn.Do("GET", "mykey") checkErr(err) bytesContent := string(bytes.([]byte)) log.Println(bytesContent)
content, err := redis.String(conn.Do("GET", "mykey")) checkErr(err) log.Println(content)
delRows, err := conn.Do("DEL", "mykey") checkErr(err) log.Println(delRows)
for i := 0; i < 3; i++ { listRows, err := conn.Do("RPUSH", "list", fmt.Sprint(i)) checkErr(err) log.Println(listRows) }
values, err := redis.Values(conn.Do("LRANGE", "list", 0, 100)) checkErr(err) for _, v := range values { log.Println(string(v.([]byte))) } conn.Do("DEL", "list") }
func checkErr(err error) { if err != nil { log.Fatalln(err) } }
|