dongzhi9192 2016-07-21 12:34
浏览 162

如何使用redis geo radius命令转到lang

I'm developing with Go lang and very new to it. I would like to use the redis GEORADIUS command and get back my results including the distance.

I have now used two packages radix.v2 and redigo to try and get results but had no joy. I have no issues using simple commands such as SET and GET.

Any help will be greatly appreciated.

Edit: added code :-)

package main

import (
  "fmt"
  "github.com/mediocregopher/radix.v2/redis"
  //"reflect"
  "encoding/json"
)

var (
  client *redis.Client
)

type Facility struct {
  Id string
  Dist string
}

func main() {
  client, err := redis.Dial("tcp", "192.168.99.100:6379")
  if err != nil {
    // handle err
  }

  surname, err := client.Cmd("GET", "surname").Str()
  if err != nil {
    // handle err
  }

  fmt.Println(surname)

  reply, _ := client.Cmd("GEORADIUS", "venues", 50, 0, 25, "mi", "WITHDIST", "ASC").Array()
  if err != nil {
    fmt.Println(err)
  }

  //fmt.Println(reflect.TypeOf(reply))
  //fmt.Println(reply)

  var facilities []Facility

  for _, results := range reply {
    facility, _ := results.Array()

    id, _ := facility[0].Str()
    dist, _ := facility[1].Str()

    facilities = append(facilities, Facility{
      Id: id,
      Dist: dist,
    })
  }

  //fmt.Println(facilities)

  resp, _ := json.Marshal(facilities)
  fmt.Fprintf(w, string(resp))    
}

Cheers Stephen

  • 写回答

1条回答 默认 最新

  • doumeitang572461 2017-02-25 16:18
    关注

    You can look at the code in the redis package.

    the file commands.go has all the signatures

    a few signatures are below

    GeoAdd(key string, geoLocation ...*GeoLocation) *IntCmd GeoPos(key string, members ...string) *GeoPosCmd GeoRadius(key string, longitude, latitude float64, query *GeoRadiusQuery) GeoRadiusByMember(key, member string, query *GeoRadiusQuery) GeoDist(key string, member1, member2, unit string) *FloatCmd

    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable