在Go语言中,静态方法的等价性

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

Equivalence of static methods in Go

问题

假设在Java中,我有一个名为CryptoFormat的类,其中有一个名为getLegacyFormat()的静态方法。当我想要使用这个方法时,只需要调用CryptoFormat.getLegacyFormat()。这很清楚,因为我知道这个方法来自哪里。

在Go语言中,没有静态方法。我不想只是创建一个名为crypto_format.go的文件并在其中定义方法。原因是每当我需要这个方法时,我只需调用GetLegacyFormat(),而不包含方法来自哪里的上下文。

我可以想到两种解决方法:

  1. 创建一个名为cryptoformat的单独包,并在该包中将方法定义为全局函数。这样,我需要为仅有几个方法创建一个新的包。而且,每当我需要这样的静态方法时,我都必须定义新的包。
  2. 定义一个名为cryptoFormat的结构体,其中包含方法GetLegacyFormat()。同时,定义一个公共变量名为CryptoFormat,它指向结构体cryptoFormat的一个实例。这样,当我需要这个方法时,我可以调用CryptoFormat.GetLegacyFormat()。

我不确定哪种方法更好,或者是否有更好的方法。

英文:

Let's say in Java, I have class CryptoFormat, which has a static method named getLegacyFormat(). When I want to use the method, I just need to call CryptoFormat.getLegacyFormat(). This is clear because I know where the method comes from.

In Go, there is no static method. I don't really want to just make a file called crypto_format.go and define the method there. The reason is that whenever I need the method, I just call GetLegacyFormat(), which doesn't contain the context where the method comes from.

I could think of two ways to solve the problem:

  1. Make a separate package named cryptoformat, and define the method as a global function in the package. This way, I need to make a new package for just few methods. Also, whenever I need static methods like this, I have to define new packages.
  2. Define a struct named cryptoFormat containing method GetLegacyFormat(). Also, define a <s>global</s> public variable named CryptoFormat, which points to an instance of struct cryptoFormat. This way, I can call CryptoFormat.GetLegacyFormat() when I need the method.

I am not sure which one is better, or whether there is better way.

答案1

得分: 10

我认为你提到的选项1是定义这种函数的更习惯的方式,如果它们不需要任何状态,这些状态不需要与底层结构绑定。

如果有一些状态你想要作为函数的上下文,那么选项2是更好的选择。

请注意,在Go语言中,函数是"一等公民",所以你不需要像Java那样定义一个类来实现静态方法的限制。

是的,如果你想要一个单独的命名空间,你需要定义单独的包(就像在Java中你需要定义单独的类和/或包)。

如果你想要你的实现符合惯用方式,我建议你看一下Go语言的标准库(选择几个包并探索它们如何实现它们的函数),以便更好地了解通常的结构方式。

英文:

I would say option 1 you mention is the more idiomatic way to define such functions, if they don't need any state that would warrant to tie them to an underlying struct.

If there is some state you'd like to have as context for the function, then option 2 would be the way to go.

Note that in Go, functions are "first class citizens", so you don't have Java's constraints of needing to define a class for static methods.

And yes, if you want a separate namespace you'd need to define separate packages (just as in Java you'd need to define separate classes and/or packages).

If you want your implementation to be idiomatic, I'd suggest you take a look at Go's standard libraries (pick a few packages and explore how they implement their functions) to get a better feeling of the usual ways to structure this.

答案2

得分: 5

每当我需要这个方法时,我只需调用GetLegacyFormat(),它不包含方法来自哪个上下文的信息。

所以在函数名中添加上下文。

GetLegacyCryptoFormat()
英文:

> whenever I need the method, I just call GetLegacyFormat(), which doesn't contain the context where the method comes from.

So add context to the function name.

GetLegacyCryptoFormat()

huangapple
  • 本文由 发表于 2017年7月9日 08:12:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/44991854.html
匿名

发表评论

匿名网友

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

确定