...

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

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

     1  // EXAMPLE: hash_tutorial
     2  // HIDE_START
     3  package example_commands_test
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  
     9  	"github.com/redis/go-redis/v9"
    10  )
    11  
    12  // HIDE_END
    13  
    14  func ExampleClient_set_get_all() {
    15  	ctx := context.Background()
    16  
    17  	rdb := redis.NewClient(&redis.Options{
    18  		Addr:     "localhost:6379",
    19  		Password: "", // no password docs
    20  		DB:       0,  // use default DB
    21  	})
    22  
    23  	// REMOVE_START
    24  	// make sure we are working with fresh database
    25  	rdb.FlushDB(ctx)
    26  	rdb.Del(ctx, "bike:1")
    27  	// REMOVE_END
    28  
    29  	// STEP_START set_get_all
    30  	hashFields := []string{
    31  		"model", "Deimos",
    32  		"brand", "Ergonom",
    33  		"type", "Enduro bikes",
    34  		"price", "4972",
    35  	}
    36  
    37  	res1, err := rdb.HSet(ctx, "bike:1", hashFields).Result()
    38  
    39  	if err != nil {
    40  		panic(err)
    41  	}
    42  
    43  	fmt.Println(res1) // >>> 4
    44  
    45  	res2, err := rdb.HGet(ctx, "bike:1", "model").Result()
    46  
    47  	if err != nil {
    48  		panic(err)
    49  	}
    50  
    51  	fmt.Println(res2) // >>> Deimos
    52  
    53  	res3, err := rdb.HGet(ctx, "bike:1", "price").Result()
    54  
    55  	if err != nil {
    56  		panic(err)
    57  	}
    58  
    59  	fmt.Println(res3) // >>> 4972
    60  
    61  	cmdReturn := rdb.HGetAll(ctx, "bike:1")
    62  	res4, err := cmdReturn.Result()
    63  
    64  	if err != nil {
    65  		panic(err)
    66  	}
    67  
    68  	fmt.Println(res4)
    69  	// >>> map[brand:Ergonom model:Deimos price:4972 type:Enduro bikes]
    70  
    71  	type BikeInfo struct {
    72  		Model string `redis:"model"`
    73  		Brand string `redis:"brand"`
    74  		Type  string `redis:"type"`
    75  		Price int    `redis:"price"`
    76  	}
    77  
    78  	var res4a BikeInfo
    79  
    80  	if err := cmdReturn.Scan(&res4a); err != nil {
    81  		panic(err)
    82  	}
    83  
    84  	fmt.Printf("Model: %v, Brand: %v, Type: %v, Price: $%v\n",
    85  		res4a.Model, res4a.Brand, res4a.Type, res4a.Price)
    86  	// >>> Model: Deimos, Brand: Ergonom, Type: Enduro bikes, Price: $4972
    87  	// STEP_END
    88  
    89  	// Output:
    90  	// 4
    91  	// Deimos
    92  	// 4972
    93  	// map[brand:Ergonom model:Deimos price:4972 type:Enduro bikes]
    94  	// Model: Deimos, Brand: Ergonom, Type: Enduro bikes, Price: $4972
    95  }
    96  
    97  func ExampleClient_hmget() {
    98  	ctx := context.Background()
    99  
   100  	rdb := redis.NewClient(&redis.Options{
   101  		Addr:     "localhost:6379",
   102  		Password: "", // no password docs
   103  		DB:       0,  // use default DB
   104  	})
   105  
   106  	// REMOVE_START
   107  	// start with fresh database
   108  	rdb.FlushDB(ctx)
   109  	rdb.Del(ctx, "bike:1")
   110  	// REMOVE_END
   111  
   112  	hashFields := []string{
   113  		"model", "Deimos",
   114  		"brand", "Ergonom",
   115  		"type", "Enduro bikes",
   116  		"price", "4972",
   117  	}
   118  
   119  	_, err := rdb.HSet(ctx, "bike:1", hashFields).Result()
   120  
   121  	if err != nil {
   122  		panic(err)
   123  	}
   124  
   125  	// STEP_START hmget
   126  	cmdReturn := rdb.HMGet(ctx, "bike:1", "model", "price")
   127  	res5, err := cmdReturn.Result()
   128  
   129  	if err != nil {
   130  		panic(err)
   131  	}
   132  
   133  	fmt.Println(res5) // >>> [Deimos 4972]
   134  
   135  	type BikeInfo struct {
   136  		Model string `redis:"model"`
   137  		Brand string `redis:"-"`
   138  		Type  string `redis:"-"`
   139  		Price int    `redis:"price"`
   140  	}
   141  
   142  	var res5a BikeInfo
   143  
   144  	if err := cmdReturn.Scan(&res5a); err != nil {
   145  		panic(err)
   146  	}
   147  
   148  	fmt.Printf("Model: %v, Price: $%v\n", res5a.Model, res5a.Price)
   149  	// >>> Model: Deimos, Price: $4972
   150  	// STEP_END
   151  
   152  	// Output:
   153  	// [Deimos 4972]
   154  	// Model: Deimos, Price: $4972
   155  }
   156  
   157  func ExampleClient_hincrby() {
   158  	ctx := context.Background()
   159  
   160  	rdb := redis.NewClient(&redis.Options{
   161  		Addr:     "localhost:6379",
   162  		Password: "", // no password docs
   163  		DB:       0,  // use default DB
   164  	})
   165  
   166  	// REMOVE_START
   167  	// start with fresh database
   168  	rdb.FlushDB(ctx)
   169  	rdb.Del(ctx, "bike:1")
   170  	// REMOVE_END
   171  
   172  	hashFields := []string{
   173  		"model", "Deimos",
   174  		"brand", "Ergonom",
   175  		"type", "Enduro bikes",
   176  		"price", "4972",
   177  	}
   178  
   179  	_, err := rdb.HSet(ctx, "bike:1", hashFields).Result()
   180  
   181  	if err != nil {
   182  		panic(err)
   183  	}
   184  
   185  	// STEP_START hincrby
   186  	res6, err := rdb.HIncrBy(ctx, "bike:1", "price", 100).Result()
   187  
   188  	if err != nil {
   189  		panic(err)
   190  	}
   191  
   192  	fmt.Println(res6) // >>> 5072
   193  
   194  	res7, err := rdb.HIncrBy(ctx, "bike:1", "price", -100).Result()
   195  
   196  	if err != nil {
   197  		panic(err)
   198  	}
   199  
   200  	fmt.Println(res7) // >>> 4972
   201  	// STEP_END
   202  
   203  	// Output:
   204  	// 5072
   205  	// 4972
   206  }
   207  
   208  func ExampleClient_incrby_get_mget() {
   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, "bike:1:stats")
   221  	// REMOVE_END
   222  
   223  	// STEP_START incrby_get_mget
   224  	res8, err := rdb.HIncrBy(ctx, "bike:1:stats", "rides", 1).Result()
   225  
   226  	if err != nil {
   227  		panic(err)
   228  	}
   229  
   230  	fmt.Println(res8) // >>> 1
   231  
   232  	res9, err := rdb.HIncrBy(ctx, "bike:1:stats", "rides", 1).Result()
   233  
   234  	if err != nil {
   235  		panic(err)
   236  	}
   237  
   238  	fmt.Println(res9) // >>> 2
   239  
   240  	res10, err := rdb.HIncrBy(ctx, "bike:1:stats", "rides", 1).Result()
   241  
   242  	if err != nil {
   243  		panic(err)
   244  	}
   245  
   246  	fmt.Println(res10) // >>> 3
   247  
   248  	res11, err := rdb.HIncrBy(ctx, "bike:1:stats", "crashes", 1).Result()
   249  
   250  	if err != nil {
   251  		panic(err)
   252  	}
   253  
   254  	fmt.Println(res11) // >>> 1
   255  
   256  	res12, err := rdb.HIncrBy(ctx, "bike:1:stats", "owners", 1).Result()
   257  
   258  	if err != nil {
   259  		panic(err)
   260  	}
   261  
   262  	fmt.Println(res12) // >>> 1
   263  
   264  	res13, err := rdb.HGet(ctx, "bike:1:stats", "rides").Result()
   265  
   266  	if err != nil {
   267  		panic(err)
   268  	}
   269  
   270  	fmt.Println(res13) // >>> 3
   271  
   272  	res14, err := rdb.HMGet(ctx, "bike:1:stats", "crashes", "owners").Result()
   273  
   274  	if err != nil {
   275  		panic(err)
   276  	}
   277  
   278  	fmt.Println(res14) // >>> [1 1]
   279  	// STEP_END
   280  
   281  	// Output:
   282  	// 1
   283  	// 2
   284  	// 3
   285  	// 1
   286  	// 1
   287  	// 3
   288  	// [1 1]
   289  }
   290  

View as plain text