...

Source file src/github.com/redis/go-redis/v9/doctests/cmds_hash_test.go

Documentation: github.com/redis/go-redis/v9/doctests

     1  // EXAMPLE: cmds_hash
     2  // HIDE_START
     3  package example_commands_test
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  	"sort"
     9  
    10  	"github.com/redis/go-redis/v9"
    11  )
    12  
    13  // HIDE_END
    14  
    15  func ExampleClient_hset() {
    16  	ctx := context.Background()
    17  
    18  	rdb := redis.NewClient(&redis.Options{
    19  		Addr:     "localhost:6379",
    20  		Password: "", // no password docs
    21  		DB:       0,  // use default DB
    22  	})
    23  
    24  	// REMOVE_START
    25  	// make sure we are working with fresh database
    26  	rdb.FlushDB(ctx)
    27  	rdb.Del(ctx, "myhash")
    28  	// REMOVE_END
    29  
    30  	// STEP_START hset
    31  	res1, err := rdb.HSet(ctx, "myhash", "field1", "Hello").Result()
    32  
    33  	if err != nil {
    34  		panic(err)
    35  	}
    36  
    37  	fmt.Println(res1) // >>> 1
    38  
    39  	res2, err := rdb.HGet(ctx, "myhash", "field1").Result()
    40  
    41  	if err != nil {
    42  		panic(err)
    43  	}
    44  
    45  	fmt.Println(res2) // >>> Hello
    46  
    47  	res3, err := rdb.HSet(ctx, "myhash",
    48  		"field2", "Hi",
    49  		"field3", "World",
    50  	).Result()
    51  
    52  	if err != nil {
    53  		panic(err)
    54  	}
    55  
    56  	fmt.Println(res3) // >>> 2
    57  
    58  	res4, err := rdb.HGet(ctx, "myhash", "field2").Result()
    59  
    60  	if err != nil {
    61  		panic(err)
    62  	}
    63  
    64  	fmt.Println(res4) // >>> Hi
    65  
    66  	res5, err := rdb.HGet(ctx, "myhash", "field3").Result()
    67  
    68  	if err != nil {
    69  		panic(err)
    70  	}
    71  
    72  	fmt.Println(res5) // >>> World
    73  
    74  	res6, err := rdb.HGetAll(ctx, "myhash").Result()
    75  
    76  	if err != nil {
    77  		panic(err)
    78  	}
    79  
    80  	keys := make([]string, 0, len(res6))
    81  
    82  	for key, _ := range res6 {
    83  		keys = append(keys, key)
    84  	}
    85  
    86  	sort.Strings(keys)
    87  
    88  	for _, key := range keys {
    89  		fmt.Printf("Key: %v, value: %v\n", key, res6[key])
    90  	}
    91  	// >>> Key: field1, value: Hello
    92  	// >>> Key: field2, value: Hi
    93  	// >>> Key: field3, value: World
    94  	// STEP_END
    95  
    96  	// Output:
    97  	// 1
    98  	// Hello
    99  	// 2
   100  	// Hi
   101  	// World
   102  	// Key: field1, value: Hello
   103  	// Key: field2, value: Hi
   104  	// Key: field3, value: World
   105  }
   106  
   107  func ExampleClient_hget() {
   108  	ctx := context.Background()
   109  
   110  	rdb := redis.NewClient(&redis.Options{
   111  		Addr:     "localhost:6379",
   112  		Password: "", // no password docs
   113  		DB:       0,  // use default DB
   114  	})
   115  
   116  	// REMOVE_START
   117  	// start with fresh database
   118  	rdb.FlushDB(ctx)
   119  	rdb.Del(ctx, "myhash")
   120  	// REMOVE_END
   121  
   122  	// STEP_START hget
   123  	res7, err := rdb.HSet(ctx, "myhash", "field1", "foo").Result()
   124  
   125  	if err != nil {
   126  		panic(err)
   127  	}
   128  
   129  	fmt.Println(res7) // >>> 1
   130  
   131  	res8, err := rdb.HGet(ctx, "myhash", "field1").Result()
   132  
   133  	if err != nil {
   134  		panic(err)
   135  	}
   136  
   137  	fmt.Println(res8) // >>> foo
   138  
   139  	res9, err := rdb.HGet(ctx, "myhash", "field2").Result()
   140  
   141  	if err != nil {
   142  		fmt.Println(err)
   143  	}
   144  
   145  	fmt.Println(res9) // >>> <empty string>
   146  	// STEP_END
   147  
   148  	// Output:
   149  	// 1
   150  	// foo
   151  	// redis: nil
   152  }
   153  
   154  func ExampleClient_hgetall() {
   155  	ctx := context.Background()
   156  
   157  	rdb := redis.NewClient(&redis.Options{
   158  		Addr:     "localhost:6379",
   159  		Password: "", // no password
   160  		DB:       0,  // use default DB
   161  	})
   162  
   163  	// REMOVE_START
   164  	// start with fresh database
   165  	rdb.FlushDB(ctx)
   166  	rdb.Del(ctx, "myhash")
   167  	// REMOVE_END
   168  
   169  	// STEP_START hgetall
   170  	hGetAllResult1, err := rdb.HSet(ctx, "myhash",
   171  		"field1", "Hello",
   172  		"field2", "World",
   173  	).Result()
   174  
   175  	if err != nil {
   176  		panic(err)
   177  	}
   178  
   179  	fmt.Println(hGetAllResult1) // >>> 2
   180  
   181  	hGetAllResult2, err := rdb.HGetAll(ctx, "myhash").Result()
   182  
   183  	if err != nil {
   184  		panic(err)
   185  	}
   186  
   187  	keys := make([]string, 0, len(hGetAllResult2))
   188  
   189  	for key, _ := range hGetAllResult2 {
   190  		keys = append(keys, key)
   191  	}
   192  
   193  	sort.Strings(keys)
   194  
   195  	for _, key := range keys {
   196  		fmt.Printf("Key: %v, value: %v\n", key, hGetAllResult2[key])
   197  	}
   198  	// >>> Key: field1, value: Hello
   199  	// >>> Key: field2, value: World
   200  	// STEP_END
   201  
   202  	// Output:
   203  	// 2
   204  	// Key: field1, value: Hello
   205  	// Key: field2, value: World
   206  }
   207  
   208  func ExampleClient_hvals() {
   209  	ctx := context.Background()
   210  
   211  	rdb := redis.NewClient(&redis.Options{
   212  		Addr:     "localhost:6379",
   213  		Password: "", // no password docs
   214  		DB:       0,  // use default DB
   215  	})
   216  
   217  	// REMOVE_START
   218  	// start with fresh database
   219  	rdb.FlushDB(ctx)
   220  	rdb.Del(ctx, "myhash")
   221  	// REMOVE_END
   222  
   223  	// STEP_START hvals
   224  	hValsResult1, err := rdb.HSet(ctx, "myhash",
   225  		"field1", "Hello",
   226  		"field2", "World",
   227  	).Result()
   228  
   229  	if err != nil {
   230  		panic(err)
   231  	}
   232  
   233  	fmt.Println(hValsResult1) // >>> 2
   234  
   235  	hValsResult2, err := rdb.HVals(ctx, "myhash").Result()
   236  
   237  	if err != nil {
   238  		panic(err)
   239  	}
   240  
   241  	sort.Strings(hValsResult2)
   242  
   243  	fmt.Println(hValsResult2) // >>> [Hello World]
   244  	// STEP_END
   245  
   246  	// Output:
   247  	// 2
   248  	// [Hello World]
   249  }
   250  

View as plain text