我可以在多个源目录中开发go软件包吗? [英] Can I develop a go package in multiple source directories?

查看:40
本文介绍了我可以在多个源目录中开发go软件包吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个go软件包,它有点复杂,因此我想将源代码组织到多个目录中.

I am developing a go package, which is a little bit complex and thus I want to organize the source code into multiple directories.

但是,我不希望软件包的用户使用太长的导入.无论如何,软件包的内部结构不是他们关心的问题.

However, I don't want the users of the package to have to use too long imports. Anyways, the internal structure of the package isn't their concern.

因此,我的包装结构如下:

Thus, my package structure looks so:

subDir1
  subSubDir1
  subSubDir2
subDir2
  subSubDir3

...等等.他们所有人都有他们的出口电话.

...and so on. All of them have their exported calls.

我想避免我的用户必须导入

I would like to avoid that my users have to import

import (
  "mypackage/subDir1"
  "mypackage/subDir1/subSubDir2"
)

...等等.

我只想要,如果他们想使用我包中的导出功能,只需导入mypackage,他们就应该可以访问所有这些文件.

I only want, if they want to use an exported function from my package, they should have access all of them by simply importing mypackage.

我尝试在所有.go文件中声明package mypackage.因此,我的源文件位于不同的目录中,但具有相同的包声明.

I tried that I declare package mypackage in all of the .go files. Thus, I had source files in different directories, but with the same package declaration.

在这种情况下,我遇到的问题是我根本无法从同一包中导入多个目录.它说:

In this case, the problem what I've confronted was that I simply couldn't import multiple directories from the same package. It said:

./src1.go:6:15: error: redefinition of ‘mypackage’
   "mypackage/mysubdir1"
               ^
./src1.go:4:10: note: previous definition of ‘mypackage’ was here
   "mypackage"
          ^
./src1.go:5:15: error: redefinition of ‘mypackage’
   "mypackage/mysubdir2"
               ^
./src1.go:4:10: note: previous definition of ‘mypackage’ was here
   "mypackage"
          ^

有什么可能吗?

推荐答案

在任何情况下都不应这样做,因为语言规范允许编译器实现拒绝此类构造.引用自规范:Package子句:

You should not do this in any case, as the language spec allows a compiler implementation to reject such constructs. Quoting from Spec: Package clause:

一组共享相同PackageName的文件构成了一个包的实现. 实施可能要求包的所有源文件都驻留在同一目录中.

代替结构化"文件名来模仿文件夹结构;例如而不是

Instead "structure" your file names to mimic the folder structure; e.g. instead of files of

foo/foo1.go
foo/bar/bar1.go
foo/bar/bar2.go

您可以简单地使用:

foo/foo1.go
foo/bar-bar1.go
foo/bar-bar2.go

如果您的程序包太大,甚至需要多个文件夹来托管"该程序包实现的文件,那么您应该真正考虑不将其实现为单个程序包,而是将其分成多个程序包.

Also if your package is so big that you would need multiple folders to even "host" the files of the package implementation, you should really consider not implementing it as a single package, but break it into multiple packages.

还要注意,转到1.5 引入了

Also note that Go 1.5 introduced internal packages. If you create a special internal subfolder inside your package folder, you may create any number of subpackages inside that (even using multiple levels). Your package will be able to import and use them (or to be more precise all packages rooted at your package folder), but no one else outside will be able to do so, it would be a compile time error.

例如您可以创建一个foo软件包,一个foo/foo.go文件和一个foo/internal/bar软件包. foo将能够导入foo/internal/bar,但是例如boo不会.另外,foo/baz也将能够导入和使用foo/internal/bar,因为它的根源是foo/.

E.g. you may create a foo package, have a foo/foo.go file, and foo/internal/bar package. foo will be able to import foo/internal/bar, but e.g. boo won't. Also foo/baz will also be able to import and use foo/internal/bar because it's rooted at foo/.

因此,您可以使用内部软件包将大软件包分解为较小的软件包,从而有效地将源文件分组到多个文件夹中.唯一需要注意的就是将您的程序包要导出的所有内容放入该程序包中,而不是放入内部程序包中(因为这些程序包是不可导入的,或者从外部"可见).

So you may use internal packages to break down your big package into smaller ones, effectively grouping your source files into multiple folders. Only thing you have to pay attention to is to put everything your package wants to export into the package and not into the internal packages (as those are not importable / visible from the "outside").

这篇关于我可以在多个源目录中开发go软件包吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆