You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.3 KiB

package main
import (
"context"
"fmt"
"github.com/go-redis/redis/v9"
)
func main() {
opt, err := redis.ParseURL("redis://default:yourPassword@192.168.157.135:6379/0?dial_timeout=1")
if err != nil {
panic(err)
}
client := redis.NewClient(opt)
ctx := context.Background()
result := client.HSet(ctx, "students", "42001", "Mashibing", "42002", "GoLang", "42003", "Redis")
fmt.Println(result.Result())
fmt.Println(client.HGetAll(ctx, "students").Result())
fmt.Println(
client.HLen(ctx, "students").
Result()) // 3 <nil>
fmt.Println(
client.HRandField(ctx, "students", 2).
Result()) // [42003 42001] <nil>, [42002 42003] <nil>
fmt.Println(
client.HRandField(ctx, "students", -2).
Result()) // [42003 42001] <nil>, [42003 42003] <nil>
fmt.Println(
client.HRandFieldWithValues(ctx, "students", 2).
Result()) // [{42002 GoLang} {42003 Redis}] <nil>
fmt.Println(
client.HExists(ctx, "students", "42003").
Result()) // true <nil>
fmt.Println(
client.HExists(ctx, "students", "42006").
Result()) // false <nil>
fmt.Println(
client.HDel(ctx, "students", "42003").
Result()) // 1 <nil>
fmt.Println(
client.HExists(ctx, "students", "42003").
Result()) // false <nil>
fmt.Println(
client.Do(ctx, "HSTRLEN", "students", "42001").
Result()) // 9 <nil>
}