package redis import ( r "github.com/garyburd/redigo/redis" "fmt" ) var ( key = "bits" p = &Pool{r.Pool{ Dial: func() (r.Conn, error) { return r.Dial("tcp", "127.0.0.1:6379") }, }} ) func ExamplePool_GetBitRange() { p.ExecuteCMD("DEL", key) p.ExecuteCMD("SETBIT", key, 1, 1) bits, _ := p.GetBitRange(key, 0, 8) fmt.Println(bits) // Output: [1] } func TestPool_GetBitRange(t *testing.T) { // 测试边界 11 [12 13] 14 p.ExecuteCMD("SETBIT", key, 11, 1) p.ExecuteCMD("SETBIT", key, 12, 1) p.ExecuteCMD("SETBIT", key, 14, 1) bits, _ := p.GetBitRange(key, 12, 2) if intInSlice(11, bits) && intInSlice(14, bits) && !intInSlice(12, bits) { t.Error("error in test TestGetBitRange") } } // intInSlice 数字是否在数组中 func intInSlice(a int, list []int) bool { for _, i := range list { if i == a { return true } } return false }