Is it possible to two have pacakage name in a same folder in golang

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

Is it possible to two have pacakage name in a same folder in golang

问题

我是一个对golang不熟悉的新手。

在node.js中,我通常会将我的通用函数组织在lib文件夹中,例如:

lib/
  validation.js
  convert.js
  ..

我想在golang中做同样的事情。

我尝试了以下方式:

lib/
 validation.go (包名为validator)
 convert.go (包名为converter)

在位于src文件夹根目录下的main.go中,我使用"./lib"进行导入,只有当两个包的名称相同时才能正常工作,否则会报错。

由于我需要使用不同的名称来调用这些函数,所以我不能给它们相同的包名,因为这会影响可读性。

所以基本上我可以在lib文件夹中创建另一个文件夹,并给它们不同的包名。这是一种可行的选择,还是有其他可能性呢?

请给出一个好的实践和最佳方式。

英文:

I am a new person to golang.

Basically in node.js i used to organise my general functions like i will put in

lib/
  validation.js
  convert.js
  ..

Likewise i will organize.
I need to do the same here..

I tried like

lib/
 validation.go ( package name validator )
 convert.go ( package name converter )

and in the main.go which is present at the root level of my src folder
i import like "./lib" it works only if both package name is same else it throws error.

since i need separate names to call those function i cant give same packages coz of readability.

so basically i can create another folder in lib and with different Package name i can give.
This is the option available or some other possibility is there.

Please suggest a good practice and optimum way.

答案1

得分: 6

根据golang.org上的《Effective Go》文档:

另一个约定是包名与其源代码目录的基本名称相同。

英文:

Per the "Effective Go" documentation on golang.org:

> Another convention is that the package name is the base name of its source directory;

huangapple
  • 本文由 发表于 2014年3月2日 23:16:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/22129380.html
匿名

发表评论

匿名网友

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

确定