GO ‘split’ 包配置设置

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

GO 'split' package configuration setup

问题

Go新手警告:

我创建了一个包,我想将其拆分为几个单独的文件,但我希望布局如下:

--src
    |---myLib.go
    |---myLib
          |--- file1.go
          |--- file2.go
          | ...
          |--- fileN.go

每个.go文件顶部都有"package myLib",但我不确定如何使路径正常工作。
主文件"myLib.go"包含"package globals"和高级函数调用(导出的),而file1、...、fileN文件实际上包含导出函数内部使用的"私有"非导出函数。

不确定如何设置导入,我尝试的每种组合都会收到消息:"no such file or directory",当我尝试在myLib文件夹中包含这些文件时。如果我将所有文件放在同一级别,那么就可以正常工作。从组织的角度来看,我更喜欢将具有导出函数的一个文件与所有非导出函数/文件合并在一个子文件夹中。有什么建议/示例吗?

谢谢

英文:

Go newbie alert:

I've created a package that I want to split up into several individual files, but I wanted to have the layout like so:

\--src
    |---myLib.go
    |---myLib
          |--- file1.go
          |--- file2.go
          | ...
          |--- fileN.go

Each .go file has "package myLib" at the top, but I'm not sure how to get the pathing to work.
The main file "myLib.go" contains the "package globals" and high level function calls (exported), while the file1, ..., fileN files essentially contain the "private" non-exported functions used internally by the exported functions.

Not sure how to setup the imports, every combination I've tried I get the message:
"no such file or directory" when I try to include the files in the myLib folder. If I put all the files at the same level, then it works fine. From an organizational standpoint, I prefer to have the one file with the exported functions separate and all the non-exported functions/files consolidated in a sub-folder. Any suggestions / examples?

Thx

答案1

得分: 3

你不能这样做。你必须按照预期的方式进行操作:

文件myLib.go必须放在myLib文件夹中。(但你可以随意命名文件夹。)

看一下标准库的组织方式。

英文:

You cannot do this. You must do it like it is intended:

The file myLib.go must go in the myLib folder. (But you may call the folder whatever you like.)

Have a look at how the standard lib is organized.

答案2

得分: 0

关键点是:同一目录中的所有.go和_test.go文件应该以相同的package <packagename>语句开头。包含main函数的目录中的.go文件应该都有package main语句-它们用于创建一个可执行文件(以目录名命名)。

文件1、...、文件N实质上包含被导出函数内部使用的“私有”非导出函数。

只有大写的函数和类型名称可以在其他包中使用(包括在package main中)。只要同一目录中的所有.go文件共享相同的package <pkgname>语句,你的设置应该可以工作-例如,如果在file1.go、file2.go... fileN.go中放置package mylib(我还建议避免使用大写的目录名以避免混淆)。

还值得查看“如何编写Go代码”文档https://golang.org/doc/code.html或其中链接的视频教程,它应该有助于理解Go代码库的布局约定。

英文:

The key piece is: all the .go and _test.go files in the same directory should start with the same package &lt;packagename&gt; statement. The .go files in the directory that contains a file with the main function should all have package main statement - they are used to create an executable (named with the directory name).

> the file1, ..., fileN files essentially contain the "private" non-exported functions used internally by the exported functions.

Only the upper-cased function and type names can be used in other packages (including in package main). The setup you have should work as long as all the .go files in the same directory share the same package &lt;pkgname&gt; statement - for example, if you put package mylib in file1.go, file2.go... fileN.go (I would also recommend to avoid uppercase directory names to avoid confusion).

It is also worth to review the "How to Write Go Code" document https://golang.org/doc/code.html or the screencast linked from it, should help with the Go codebase layout conventions.

huangapple
  • 本文由 发表于 2014年1月7日 21:59:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/20973532.html
匿名

发表评论

匿名网友

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

确定