英文:
Go compile error "undefined function"
问题
我有以下代码片段:
接口和函数定义:
package helper
import "gopkg.in/mgo.v2/bson"
// 定义可识别对象的接口
type Identifiable interface {
GetIdentifier() bson.ObjectId
}
// 检查切片中是否包含具有给定 bson.ObjectId 的对象
func IsInSlice(id bson.ObjectId, objects []Identifiable) bool {
for _, single := range objects {
if single.GetIdentifier() == id {
return true
}
}
return false
}
满足 'Identifiable' 的用户结构体定义:
package models
import (
"github.com/my/project/services"
"gopkg.in/mgo.v2/bson"
)
type User struct {
Id bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
Username string `json:"username" bson:"username"`
Email string `json:"email" bson:"email"`
Password string `json:"password" bson:"password"`
Albums []bson.ObjectId `json:"albums,omitempty" bson:"albums,omitempty"`
Friends []bson.ObjectId `json:"friends,omitempty" bson:"friends,omitempty"`
}
func GetUserById(id string) (err error, user *User) {
dbConnection, dbCollection := services.GetDbConnection("users")
defer dbConnection.Close()
err = dbCollection.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&user)
return err, user
}
func (u *User) GetIdentifier() bson.ObjectId {
return u.Id
}
检查切片中对象存在性的测试:
package controllers
import (
"github.com/my/project/helper"
"gopkg.in/mgo.v2/bson"
)
var testerId = bson.NewObjectId()
var users = []models.Users{}
/* 从数据库获取用户的一些代码 */
if !helper.IsInSlice(testerId, users) {
t.Fatalf("User is not saved in the database")
}
当我尝试编译测试时,我得到错误:undefined helper.IsInSlice
。当我将 IsInSlice
方法重写为不接受 []Identifiable
而是 []models.User
时,它可以正常工作。
有什么想法吗?
英文:
I have the following pieces of code:
Interface & function definition:
package helper
import "gopkg.in/mgo.v2/bson"
// Defines an interface for identifiable objects
type Identifiable interface {
GetIdentifier() bson.ObjectId
}
// Checks if a slice contains a given object with a given bson.ObjectId
func IsInSlice(id bson.ObjectId, objects []Identifiable) bool {
for _, single := range objects {
if single.GetIdentifier() == id {
return true
}
}
return false
}
The definition of the user struct which satisfies 'Identifiable':
package models
import (
"github.com/my/project/services"
"gopkg.in/mgo.v2/bson"
)
type User struct {
Id bson.ObjectId `json:"_id,omitempty" bson:"_id,omitempty"`
Username string `json:"username" bson:"username"`
Email string `json:"email" bson:"email"`
Password string `json:"password" bson:"password"`
Albums []bson.ObjectId `json:"albums,omitempty" bson:"albums,omitempty"`
Friends []bson.ObjectId `json:"friends,omitempty" bson:"friends,omitempty"`
}
func GetUserById(id string) (err error, user *User) {
dbConnection, dbCollection := services.GetDbConnection("users")
defer dbConnection.Close()
err = dbCollection.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&user)
return err, user
}
func (u *User) GetIdentifier() bson.ObjectId {
return u.Id
}
A test which checks for the existence of an object inside a slice:
package controllers
import (
"github.com/my/project/helper"
"gopkg.in/mgo.v2/bson"
)
var testerId = bson.NewObjectId()
var users = []models.Users{}
/* Some code to get users from db */
if !helper.IsInSlice(testerId, users) {
t.Fatalf("User is not saved in the database")
}
When I try to compile the test it I get the error: undefined helper.IsInSlice
. When I rewrite the IsInSlice
method to not take []Identifiable
but []models.User
it works fine.
Any ideas?
答案1
得分: 0
你的问题是,你试图将类型为[]models.Users{}
的值用作类型为[]Identifiable
的值。虽然models.Users
实现了Identifiable
接口,但Go的类型系统设计成实现接口的值的切片不能用作(或转换为)接口类型的切片。
请参阅Go规范中的转换部分以获取更多详细信息。
英文:
Your problem is that you're trying to use a value of type []models.Users{}
as a value of type []Identifiable
. While models.Users
implements Identifiable
, Go's type system is designed so that slices of values implementing an interface cannot be used as (or converted to) slices of the interface type.
See the Go specification's section on conversions for more details.
答案2
得分: 0
显然,Go没有重新构建我的包,并且在旧的构建中寻找该函数。因此它是未定义的。执行rm -fr [GO-ROOT]/pkg/github.com/my/project/models
就可以解决问题。
英文:
Apparently, Go did not rebuild my package and was looking for the function in an old build. It was therefore undefined. Performing rm -fr [GO-ROOT]/pkg/github.com/my/project/models
did the trick.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论