
我试图在编写断言函数来测试事物时使用泛型,但是它给了我一个错误 some does not implement testutilt (wrong type for method equals...) 错误。如果有的话我怎样才能使下面的代码工作?
package test_util
import (
"fmt"
"testing"
)
type TestUtilT interface {
Equals(TestUtilT) bool
String() string
}
func Assert[U TestUtilT](t *testing.T, location string, must, is U) {
if !is.Equals(must) {
t.Fatalf("%s expected: %s got: %s\n",
fmt.Sprintf("[%s]", location),
must,
is,
)
}
}
type Some struct {
}
func (s *Some) Equals(other Some) bool {
return true
}
func (s *Some) String() string {
return ""
}
func TestFunc(t *testing.T) {
Assert[Some](t, "", Some{}, Some{})
// Error: "Some does not implement TestUtilT (wrong type for method Equals...)"
}正确答案
替换
func (s *some) equals(other some) bool {
与
func (s *some) equals(other testutilt) bool {
然后替换
Mallz既适合作为B2C的企业电子商务网站,也可以作为C2C个人电子商务网站和多用户企业团购网站,简单来说是可以方便不同类型的用户构造适合自身的需要的网上电子商务平台构建系统。同时它是内置Mallz网站整合管理系统强大的整合模块,可以通过其整合接口轻松整合网络上任意一种的系统,可以让你轻松快捷打造一个具有门户功能的电子商务门户网站。
assert[some](t, "", some{}, some{})
与
Assert[Some](t, "", &Some{}, &Some{})
第一个更改将修复您的初始错误消息,但如果没有第二个更改,您的代码仍然无法工作。








