以下错误:
./main.go:13: c.Set undefined (type redis.Conn has no field or method Set)
./main.go:19: invalid receiver type *redis.Conn (redis.Conn is an interface type)
./main.go:20: red.Send undefined (type *redis.Conn has no field or method Send)
是从这个代码产生的:
package main
import (
"encoding/json"
"github.com/garyburd/redigo/redis"
"github.com/gorilla/mux"
"log"
"net/http"
)
func setHandler(res http.ResponseWriter, req *http.Request) {
c := connectRedis()
c.Set("foo", "bar")
data, _ := json.Marshal("{'order':1, 'weight': 100, 'reps': 5, 'rest': 1}")
res.Header().Set("Content-Type", "application/json; charset=utf-8")
res.Write(data)
}
func (red *redis.Conn) Set(key string, value string) error {
if _, err := red.Send("set", key, value); err != nil {
return err
}
}
func connectRedis() redis.Conn {
c, err := redis.Dial("tcp", ":6379")
if err != nil {
// handle error
}
defer c.Close()
return c
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/sets.json", setHandler)
http.Handle("/", r)
err := http.ListenAndServe(":7000", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
当我有一个接口时,如何注册一个方法?
慕的地6264312
aluckdog
相关分类