donglianglu8136 2015-08-13 12:06
浏览 41
已采纳

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

Hi guys fairly new to Golang, I understand that interfaces are kind of like contracts that guarantee that certain things will operate a certain way, thats cool and all, and if I make a local copy of it I can basically re-write how it operates (From what I understand, please correct me if I'm wrong)

Here is what I have so far

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)

}

Basically I am trying to make my own DiscoveryCache so that it uses a database instead of memory for storage (as instructed to do by the Go-OpenID package located here: https://github.com/yohcop/openid-go

The part I'm trying to recreate is located here: https://github.com/yohcop/openid-go/blob/master/discovery_cache.go

Now I have done (what I assume) everything that should need doing to make this work, but I keep getting this error:

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)

If anybody could inform me on what I have done wrong that would be much appreciated. Thanks! If you need any more information please let me know.

  • 写回答

1条回答 默认 最新

  • doujing6436 2015-08-13 12:14
    关注

    SimpleDiscoveredInfo doesn't implement the interface's methods, you need something like this:

    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

    For

    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)
    

    Your types need to return openid.DiscoveredInfo not DiscoveredInfo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 GD32 SPI通信时我从机原样返回收到的数据怎么弄?
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?