...

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

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

     1  // EXAMPLE: geo_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_geoadd() {
    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, "bikes:rentable")
    27  	// REMOVE_END
    28  
    29  	// STEP_START geoadd
    30  	res1, err := rdb.GeoAdd(ctx, "bikes:rentable",
    31  		&redis.GeoLocation{
    32  			Longitude: -122.27652,
    33  			Latitude:  37.805186,
    34  			Name:      "station:1",
    35  		}).Result()
    36  
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  
    41  	fmt.Println(res1) // >>> 1
    42  
    43  	res2, err := rdb.GeoAdd(ctx, "bikes:rentable",
    44  		&redis.GeoLocation{
    45  			Longitude: -122.2674626,
    46  			Latitude:  37.8062344,
    47  			Name:      "station:2",
    48  		}).Result()
    49  
    50  	if err != nil {
    51  		panic(err)
    52  	}
    53  
    54  	fmt.Println(res2) // >>> 1
    55  
    56  	res3, err := rdb.GeoAdd(ctx, "bikes:rentable",
    57  		&redis.GeoLocation{
    58  			Longitude: -122.2469854,
    59  			Latitude:  37.8104049,
    60  			Name:      "station:3",
    61  		}).Result()
    62  
    63  	if err != nil {
    64  		panic(err)
    65  	}
    66  
    67  	fmt.Println(res3) // >>> 1
    68  	// STEP_END
    69  
    70  	// Output:
    71  	// 1
    72  	// 1
    73  	// 1
    74  }
    75  
    76  func ExampleClient_geosearch() {
    77  	ctx := context.Background()
    78  
    79  	rdb := redis.NewClient(&redis.Options{
    80  		Addr:     "localhost:6379",
    81  		Password: "", // no password docs
    82  		DB:       0,  // use default DB
    83  	})
    84  
    85  	// REMOVE_START
    86  	// start with fresh database
    87  	rdb.FlushDB(ctx)
    88  	rdb.Del(ctx, "bikes:rentable")
    89  
    90  	_, err := rdb.GeoAdd(ctx, "bikes:rentable",
    91  		&redis.GeoLocation{
    92  			Longitude: -122.27652,
    93  			Latitude:  37.805186,
    94  			Name:      "station:1",
    95  		}).Result()
    96  
    97  	if err != nil {
    98  		panic(err)
    99  	}
   100  
   101  	_, err = rdb.GeoAdd(ctx, "bikes:rentable",
   102  		&redis.GeoLocation{
   103  			Longitude: -122.2674626,
   104  			Latitude:  37.8062344,
   105  			Name:      "station:2",
   106  		}).Result()
   107  
   108  	if err != nil {
   109  		panic(err)
   110  	}
   111  
   112  	_, err = rdb.GeoAdd(ctx, "bikes:rentable",
   113  		&redis.GeoLocation{
   114  			Longitude: -122.2469854,
   115  			Latitude:  37.8104049,
   116  			Name:      "station:3",
   117  		}).Result()
   118  
   119  	if err != nil {
   120  		panic(err)
   121  	}
   122  	// REMOVE_END
   123  
   124  	// STEP_START geosearch
   125  	res4, err := rdb.GeoSearch(ctx, "bikes:rentable",
   126  		&redis.GeoSearchQuery{
   127  			Longitude:  -122.27652,
   128  			Latitude:   37.805186,
   129  			Radius:     5,
   130  			RadiusUnit: "km",
   131  		},
   132  	).Result()
   133  
   134  	if err != nil {
   135  		panic(err)
   136  	}
   137  
   138  	fmt.Println(res4) // >>> [station:1 station:2 station:3]
   139  	// STEP_END
   140  
   141  	// Output:
   142  	// [station:1 station:2 station:3]
   143  }
   144  

View as plain text