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