example.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2012 Gary Burd
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"): you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  11. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  12. // License for the specific language governing permissions and limitations
  13. // under the License.
  14. // +build ignore
  15. package main
  16. import (
  17. "github.com/garyburd/redigo/redis"
  18. "github.com/garyburd/redigo/redisx"
  19. "log"
  20. )
  21. type MyStruct struct {
  22. A int
  23. B string
  24. }
  25. func main() {
  26. c, err := redis.Dial("tcp", ":6379")
  27. if err != nil {
  28. log.Fatal(err)
  29. }
  30. v0 := &MyStruct{1, "hello"}
  31. _, err = c.Do("HMSET", redisx.AppendStruct([]interface{}{"key"}, v0)...)
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. reply, err := c.Do("HGETALL", "key")
  36. if err != nil {
  37. log.Fatal(err)
  38. }
  39. v1 := &MyStruct{}
  40. err = redisx.ScanStruct(reply, v1)
  41. if err != nil {
  42. log.Fatal(err)
  43. }
  44. log.Printf("v1=%v", v1)
  45. }