0

I have a php service and a golang service. I set the value of redis in the php service and use igbinary. How to correctly GET this value in golang service.

Ginger
  • 1

1 Answers1

0

igbinary of php sets a binary value, you can read the binary value into array of []byte in go.

response,err:=redisClient.Get(ctx,"key").Bytes()

here response bytes can be unmarshalled into proper struct to get values.

whitespace
  • 789
  • 6
  • 13
  • I used php to set the value of a key in redis to be "igbinary", but the value obtained by golang in redis is "\x00\x00\x00\x02\x11\bigbinary". I can't get the correct value. – Ginger Aug 25 '22 at 05:12
  • depends on the binary data you have stored, what exactly is the data, before it is converted to binary. – whitespace Aug 25 '22 at 05:14
  • [php] $rds = new \Redis(); $rds->connect('127.0.0.1',6379,5); $rds->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_IGBINARY); $rds->set('test_igbinary_key','igbinary'); [golang] rds := redis.NewClient(options) val := rds.Get(ctx, "test_igbinary_key") b, _ := val.Bytes() fmt.Println(b) fmt.Println(string(b)) fmt.Printf("%#+v\n", string(b)) [result] [0 0 0 2 17 8 105 103 98 105 110 97 114 121] gbinary "\x00\x00\x00\x02\x11\bigbinary" – Ginger Aug 25 '22 at 05:31
  • The value set in redis is 'igbinary' – Ginger Aug 25 '22 at 05:34
  • as you can see `string(b)` gives you the value. If the value is a json, you will unmarshal it. – whitespace Aug 25 '22 at 06:40