Skip to content

Instantly share code, notes, and snippets.

@poppen
Last active January 7, 2018 05:02
Show Gist options
  • Save poppen/33a25987c7f860da2f121dc89188d95b to your computer and use it in GitHub Desktop.
Save poppen/33a25987c7f860da2f121dc89188d95b to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/garyburd/redigo/redis"
)
// Person ...
type Person struct {
ID int `json:"id"`
Name string `json:"name"`
}
func main() {
c, err := redis.DialURL("redis://localhost:6379")
if err != nil {
log.Fatal(err)
}
defer c.Close()
pj := `
{
"id": 1,
"name": "YAMADA Taro"
}
`
c.Do("SET", "1", pj)
e, err := redis.Bool(c.Do("EXISTS", "1"))
if err != nil {
log.Fatal(err)
}
if e == false {
return
}
s, err := redis.String(c.Do("GET", "1"))
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v\n", s)
b := ([]byte)(s)
p := &Person{}
err = json.Unmarshal(b, p)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%v\n", p)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment