接口/结构"不实现X,错误的类型或方法,不确定为什么我收到此错误

时间:2015-08-13 12:06:13

标签: go

你好相当Golang的新手,我明白接口有点像合约,保证某些东西会以某种方式运作,那很酷,所有,如果我制作本地副本,我基本上可以重写它是如何运作的(根据我的理解,如果我错了,请纠正我)

这是我到目前为止所拥有的

package register

import (
    "log"
    "net/http"


    "github.com/yohcop/openid-go"
)

var nonceStore = &openid.SimpleNonceStore{
    Store: make(map[string][]*openid.Nonce)}
var discoveryCache = &SimpleDiscoveryCache{}

type DiscoveredInfo interface {
    OpEndpoint() string
    OPLocalID() string
    ClaimedID() string
}

type SimpleDiscoveredInfo struct {
    opEndpoint, opLocalID, claimedID string
}

type SimpleDiscoveryCache map[string]DiscoveredInfo

func (s *SimpleDiscoveryCache) Put(id string, info DiscoveredInfo) {
    db := common.ConnectDB()

    rows, err := db.Query("INSERT INTO discovery_cache SET id=?, opendpoint=?, oplocalid=?, claimedid=?",
        id, info.OpEndpoint(), info.OPLocalID(), info.ClaimedID())

    if err != nil {
        panic("Error: " + err.Error())
    }

    log.Println(rows)
}

func (s *SimpleDiscoveryCache) Get(id string) DiscoveredInfo {
    db := common.ConnectDB()

    rows, err := db.Query("SELECT FROM discovery_cache WHERE id=?", id)
    if err != nil {
        panic("Error: " + err.Error())
    }

    log.Println(rows)

    var opEndpoint, opLocalID, claimedID string

    for rows.Next() {
        err := rows.Scan(&opEndpoint, &opLocalID, &claimedID)
        if err != nil {
            panic("Help!")
        }
    }

    return &SimpleDiscoveredInfo{
        opEndpoint, opLocalID, claimedID,
    }

}

func DiscoverHandler(w http.ResponseWriter, r *http.Request) {
    url, err := openid.RedirectURL("http://steamcommunity.com/openid", "http://localhost:1337/login/return", "http://localhost")

    if err != nil {
        http.Error(w, "Failed to login", 500)
    }

    http.Redirect(w, r, url, 303)
}

func CallbackHandler(w http.ResponseWriter, r *http.Request) {

    fullUrl := "http://localhost:1337" + r.URL.String()
    id, err := openid.Verify(fullUrl, discoveryCache, nonceStore)
    if err != nil {
        http.Error(w, "Failed", 500)
    }
    log.Println(id)

}

基本上我正在尝试创建自己的DiscoveryCache,以便它使用数据库而不是内存进行存储(按照此处的Go-OpenID包的指示进行操作:https://github.com/yohcop/openid-go

我尝试重新创建的部分位于此处:https://github.com/yohcop/openid-go/blob/master/discovery_cache.go

现在我已经完成了(我假设的)应该做的所有工作,但我一直收到这个错误:

controllers/register/register.go:60: cannot use SimpleDiscoveredInfo literal (type *SimpleDiscoveredInfo) as type openid.DiscoveredInfo in return argument:
    *SimpleDiscoveredInfo does not implement openid.DiscoveredInfo (missing ClaimedID method)
controllers/register/register.go:78: cannot use discoveryCache (type *SimpleDiscoveryCache) as type openid.DiscoveryCache in argument to openid.Verify:
    *SimpleDiscoveryCache does not implement openid.DiscoveryCache (wrong type for Put method)
        have Put(string, DiscoveredInfo)
        want Put(string, openid.DiscoveredInfo)

如果有人能告诉我我做错了什么,我将不胜感激。谢谢!如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:3)

SimpleDiscoveredInfo没有实现界面的方法,你需要这样的东西:

func (sdi *SimpleDiscoveredInfo) OpEndpoint() string { return sdi.opEndpoint }
func (sdi *SimpleDiscoveredInfo) OpLocalID() string  { return sdi.opLocalID }
func (sdi *SimpleDiscoveredInfo) ClaimedID() string  { return sdi.claimedID }

var _ openid.DiscoveredInfo = (*SimpleDiscoveredInfo)(nil)

http://play.golang.org/p/qVTTKfhNHu

对于

controllers/register/register.go:78: cannot use discoveryCache (type *SimpleDiscoveryCache) as type openid.DiscoveryCache in argument to openid.Verify:
    *SimpleDiscoveryCache does not implement openid.DiscoveryCache (wrong type for Put method)
        have Put(string, DiscoveredInfo)
        want Put(string, openid.DiscoveredInfo)

您的类型需要返回openid.DiscoveredInfo而不是DiscoveredInfo