【发布时间】:2017-07-26 02:08:06
【问题描述】:
我是 Go 语言的新手,在使用 getter 和 setter 从不同文件指定结构的接口时遇到问题。
来源
src/github.com/user/interfaces
package interfaces
type IFoo interface {
Name() string
SetName(name string)
}
src/github.com/user/foo
package foo
import "github.com/user/interfaces"
type Foo struct {
name string
}
func (f *interfaces.IFoo) SetName(name string) {
f.name = name
}
func (f IFoo) Name() string {
return f.name
}
如果我将 struct Foo 的方法签名更改为以下,则 import "github.com/user/interfaces" 将变为未使用。
func (f *Foo) SetName(name string) {
f.name = name
}
func (f Foo) Name() string {
return f.name
}
测试
test/github.com/user/foo/foo_test.go
package foo
import (
"testing"
"github.com/user/foo"
"fmt"
"github.com/user/interfaces"
)
func TestFoo(t *testing.T) {
foo := foo.Foo{}
fmt.Println(interfaces.IFoo(foo))
}
问题:我怎样才能在上面的例子中为 Foo 结构体指定 IFoo 接口并让单元测试通过?
【问题讨论】:
标签: go