MyInterface with MyImplementation in separate files of a library

huangapple go评论71阅读模式
英文:

MyInterface with MyImplementation in separate files of a library

问题

项目结构要求在单独的文件中定义接口和实现。

最好下载一个小的隔离测试案例来了解情况,但这里也有代码。这是一个库项目,所以每个文件都声明了mylib作为包。我想要有子包,但是golang不允许这样做,所以我将所有内容都放在一个包下,而文件则放在不同的目录中。

下载测试项目

src/interfaces/my_interface.go

package mylib

type MyInterface interface {
    foo()
    bar()
}

src/interfaces/my_implementation.go 这里如何导入接口?

package mylib

type MyImplementation struct {}

// 在这里声明*MyInterface可能吗?
func (imp *MyImplementation) foo() {} 

func (imp *MyImplementation) bar() {}

test/implementations/implementation_test.go

package implementations

import (
    "testing"
    "fmt"
    "implementations"
    mylib2 "interfaces"  // 为什么这里是mylib2?
)

func TestImplementation(t *testing.T) {

    // 如何声明一个类型为`MyInterface`的变量,并将一个`MyImplementation`对象赋值给它
    interface_type_var := mylib2.MyInterface() // 错误
    interface_type_var := mylib.MyImplementation{} // 错误

    fmt.Println("测试成功")
}

问题

  1. 如何声明一个类型为MyInterface的变量,并将MyImplementation对象赋值给它。

  2. 如何在实现文件中导入接口。

  3. IDE的自动完成将其放在导入语句下面,因为我正在导入接口类型。不确定为什么会这样。mylib2 "interfaces"。我了解到这是一个别名,但为什么我们在这里需要别名?

  4. 请帮忙修复implementation_test中的代码

英文:

The project structure requires to define interface and implementations in separate files.

It's best to download a small isolated test case to get a picture, but here's the code too. It's a library project that's why mylib is declared as package for each file. I wanted to have subpackages but golang doesn't allow that, so instead I put everything under one package while files are in separate directories.

Download Test Project

src/interfaces/my_interface.go

package mylib

type MyInterface interface {
    foo()
    bar()
}

src/interfaces/my_implementation.go how to import interface here?

package mylib

type MyImplementation struct {}

// possible to declare *MyInteface here?
func (imp *MyImplementation) foo() {} 

func (imp *MyImplementation) bar() {}

test/implementations/implementation_test.go

package implementations

import (
	"testing"
	"fmt"
	"implementations"
	mylib2 "interfaces"  // why mylib2 here?
)

func TestImplementation(t *testing.T) {

	// how to declare variable of type `MyInterface` and assign an object `MyImplementation` to it
	interface_type_var := mylib2.MyInterface() // error
	interface_type_var := mylib.MyImplementation{} // error

	fmt.Println("Test successful")
}

Question

  1. How can I declare a type of MyInterface and assign object of MyImplementation to it.

  2. how to import and interface in the implementation file

  3. Autocomplete of IDE put this under imports automatically as I was importing interface type. not sure why. mylib2 "interfaces". i learned it's an alias but why do we need alias here?

  4. Help fix the code in the implementation_test please

答案1

得分: 1

  1. 你已经在 src/interfaces/my_interface.go 中声明了一个 MyInterface 类型。你不需要重新声明它,也不需要显式地将 MyImplementation 分配给接口。你只需要确保你的 MyImplementation 实现了接口中的所有方法。只要这两个文件在同一个包和同一个文件夹级别下,你的实现就会自动成为接口。

  2. 只要它们在同一个包和文件夹中,你不需要在实现中导入接口。

  3. 在撰写本文时,Gogland 处于早期访问计划阶段,这只是 BETA 版本的另一个名称 :). 我不认为 Stack Overflow 允许对任何 BETA 版本的内容进行评论。抱歉。

编辑

  1. 根据你的代码结构,当尝试使用子文件夹时,你的方法有些错误。如果你使用子文件夹,这些子文件夹应该是独立的包。假设你的 GOPATH 是 ~/go。你需要这样的结构:~/go/src/github.com/user/mylib。所以如果你想要一个子文件夹/子包,你会有这样的结构:~/go/src/github.com/user/mylib/util,而 util 将是它自己的包。试图将实现放在一个文件夹中,接口放在另一个文件夹中是错误的。相反,逻辑上将它们分组并创建子包。然后在你的测试中可以这样使用它们:

import(
  "github.com/user/mylib"
  "github.com/user/mylib/util"
)

github.com 或任何其他仓库服务对于编写可在任何项目中重用的库非常重要。

除此之外,我建议使用更稳定的 IDE 或编辑器。

希望对你有所帮助。

英文:
  1. You have already declared a type of MyInterface inside src/interfaces/my_interface.go. You don't have to redeclare it and you don't have to explicitly assign MyImplementation to the interface. All you have to make sure is that your MyImplementation implements all the methods from the interface. As long as both files are in the same package and same folder level, your implementation automatically becomes your interface.

  2. As long they are both in the same package and folder you do not have to import interface inside implementation

  3. At time of this writing Gogland is in Early Access Program which is just another name for BETA :). I don't think SO allows commenting on anything in beta. Sorry.

EDIT

  1. Based on the structure of your code, you are taking bit wrong approach when trying to use subfolders. If you are using subfolders, those subfolders should be independent packages. So let's say your GOPATH is ~/go. You need a structure like this: ~/go/src/github.com/user/mylib. So if you want a subfolder/subpackage you would have something like this ~/go/src/github.com/user/mylib/util and util would be its own package. It's wrong to try to have Implementations in one folder and interfaces in another. Rather group them logically and create subpackages. Then in your test you can use them like this:

import(
  "github.com/user/mylib"
  "github.com/user/mylib/util"
)

github.com or any other repo service is important specially if you are writing library that is suppose to be reusable in any project.

Besides that, I'd suggest to use more stable IDE or editor.

Hope this helps

huangapple
  • 本文由 发表于 2017年3月9日 10:32:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/42685620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定