Go 接口/结构“;没有实现X,类型或方法错误,不确定为什么会出现此错误
大家好,刚到Golang的家伙们,我知道接口有点像合同,保证某些东西会以某种方式运行,这很酷,如果我在本地复制它,我基本上可以重写它的运行方式(根据我的理解,如果我错了,请纠正我) 这是我到目前为止所拥有的Go 接口/结构“;没有实现X,类型或方法错误,不确定为什么会出现此错误,go,Go,大家好,刚到Golang的家伙们,我知道接口有点像合同,保证某些东西会以某种方式运行,这很酷,如果我在本地复制它,我基本上可以重写它的运行方式(根据我的理解,如果我错了,请纠正我) 这是我到目前为止所拥有的 package register import ( "log" "net/http" "github.com/yohcop/openid-go" ) var nonceStore = &openid.SimpleNonceStore{ Stor
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包的指示,位于此处:
我尝试重新创建的部分位于此处:
现在,我已经做了(我假设的)一切应该做的事情,以使这项工作,但我不断得到这个错误:
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)
为了
您的类型需要返回
openid.DiscoveredInfo
notDiscoveredInfo
为什么?为什么要像(*SimpleDiscoveredInfo)(nil)那样包装它?在实现接口时,执行var.interfaceName=(*structName)(nil)
验证您的类型是否在不分配更多空间的情况下实现了接口。感谢您的回复,我现在在床上,如果它在早上工作,我将检查此作为答案!嘿,我在这里发布了一个类似的问题:如果您也能在那里提供帮助!:)再次感谢
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)