登录
首页 >  Golang >  Go问答

接口/结构“没有实现 X,错误的类型或方法,不知道为什么我会收到这个错误

来源:Golang技术栈

时间:2023-03-08 15:50:12 275浏览 收藏

怎么入门Golang编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《接口/结构“没有实现 X,错误的类型或方法,不知道为什么我会收到这个错误》,涉及到golang,有需要的可以收藏一下

问题内容

嗨,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](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)

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

正确答案

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.DiscoveredInfonot DiscoveredInfo

理论要掌握,实操不能落!以上关于《接口/结构“没有实现 X,错误的类型或方法,不知道为什么我会收到这个错误》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:Golang技术栈 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>