When writing a single package meant to be used as a command, which is idiomatic: name all identifiers as private or name all identifiers as public?

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

When writing a single package meant to be used as a command, which is idiomatic: name all identifiers as private or name all identifiers as public?

问题

在Go语言中,公共名称以大写字母开头,私有名称以小写字母开头。

我正在编写一个不是库而是单个包的程序。是否有任何Go语言习惯用法规定我的标识符应该全部是公共的还是全部是私有的?我不打算将此包用作库,也不打算从另一个Go程序中导入它。

我想不出任何理由为什么我会想要混合使用。感觉上,全部使用私有是正确的选择。


我不认为我得到了任何具体的答案,但Nate最接近了,他告诉我要考虑“导出与非导出”,而不是“公共和私有”。

这让我相信不导出任何东西是最好的方法。在最坏的情况下,如果我最终在另一个包中导入我的应用程序的代码,我将不得不重新考虑应该导出什么和不应该导出什么。这在我看来是一件好事。

英文:

In Go, public names start with an upper case letter and private names start with a lower case letter.

I'm writing a program that is not library and is a single package. Is there any Go idiom that stipulates whether my identifiers should be all public or all private? I do not plan on using this package as a library or as something that should be imported from another Go program.

I can't think of any reason why I'd want a mixture. It "feels" like going all private is the proper choice.


I don't think I got any concrete answer, but Nate was closest with telling me to think of "exporting vs non-exporting" instead of "public and private".

This leads me to believe that not exporting anything is the best approach. In the worst case scenario, if I end up importing code from my application in another package, I will have to rethink what should be exported and what shouldn't be. Which is a good thing IMO.

答案1

得分: 3

如果您试图调整您的思维方式以更符合Go的习惯用法,您应该停止将变量、函数和方法视为公共或私有。更准确的术语是导出或未导出。这确实更像是C语言的感觉。

正如其他人所说,导出对于应用程序代码来说并不是必需的。如果出于组织原因,您决定将程序分解为包,您可以使用子包。在我们的工作中,我们决定就是这样做的。我们有:

<pre><code>projectgopath/src/projectname
projectname/subcomponent1
projectname/subcomponent2
</code></pre>

到目前为止,我真的很喜欢这个结构。它有助于关注点的分离,但不会超出主项目之外的包。意图很明确。子包的预期用途仅限于此程序...

新的go build和go install命令似乎处理得非常好。我们将组件组合在一起放在包中,并通过导出仅暴露必要的部分。

英文:

If you are attempting to adjust your mindset to be more Go idiomatic, you should stop thinking of variables, functions, and methods as public or private. The more accurate term is exported or not exported. It definitely has a more C like feel to it.

As others have stated exporting really isn't needed for application program code. If for organizational reasons you decide to break your program up into packages, you could use sub-packages. At work we've decided to do just this. We have:

<pre><code>projectgopath/src/projectname
projectname/subcomponent1
projectname/subcomponent2
</code></pre>

So far I am really liking this structure. It aids in separation of concerns, but does not go to the extent of making a package outside of the main project. The intent is clear. The sub-package's intended use is for this program only...

The new go build and go install commands seem to deal very well with it. We group components together in packages and expose only the necessary bits via exports.

答案2

得分: 2

在所描述的情况下,这两种方法都是同样有效的,所以这更多是个人偏好的问题。在我的情况下,我在主要包中使用驼峰命名法标识符,主要是出于习惯。

英文:

In the described situation both approaches are equally valid, so it's more or less a matter of personal preferences. In my case I'm using camelCase identifiers for package main, mostly out of habit.

答案3

得分: 1

很多我的go文件最初是在独立的命令中创建的,后来被移动到包中,以便可以被同一主题周围的几个命令重用。

我认为你应该将那些不可能从其他地方调用的内容都设为私有(假设有一天你将其变为可导入的包),并且将可以从其他地方理解的大型函数(如果有的话)和结构体字段设为公共(当它们是正交的时候,也就是说,当一个字段的值的改变不会破坏结构体值的一致性时)。

英文:

A lot of my go files started their life in isolated commands and were moved to packages as they could be reused by a few commands around the same topic.

I think you should make private all that couldn't possibly be called from elsewhere (supposing one day you make it an importable package) and make public the big functions that can be understood from elsewhere (if any) and structs fields when they are orthogonal (I mean when a change of the value of one field doesn't break the consistency of the struct value).

huangapple
  • 本文由 发表于 2012年3月13日 09:45:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/9677297.html
匿名

发表评论

匿名网友

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

确定