gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 01:39:30 24 4
gpt4 key购买 nike

大家好,刚接触 Golang,我知道接口(interface)有点像契约,保证某些事情将以某种方式运行,这很酷,如果我制作它的本地副本,我基本上可以重写它是如何运作的(据我了解,如果我错了请指正)

这是我目前的情况

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)

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

最佳答案

SimpleDiscoveredInfo 没有实现接口(interface)的方法,你需要这样的东西:

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

关于go - 接口(interface)/结构“没有实现 X,错误的类型或方法,不知道为什么我会收到这个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31987769/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com