duanjue2560 2017-08-26 20:00
浏览 29
已采纳

我可以在多个源目录中开发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"
)

...and so on.

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.

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"
          ^

Is it somehow possible?

  • 写回答

3条回答 默认 最新

  • dongshui2254 2017-08-27 06:52
    关注

    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:

    A set of files sharing the same PackageName form the implementation of a package. An implementation may require that all source files for a package inhabit the same directory.

    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
    

    You could simply use:

    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.

    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.

    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").

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100