在Go语言中的继承

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

Inheritance in golang

问题

我正在尝试创建一个模板方法,在调用某个特定的方法时执行该方法。

例如:

func main() {
   onInit()
}

func onInit() {
   var Instance entity.EntityInstance
   Instance.Init()
   //做一些事情
}

另一个源文件,instance.go:

type EntityInstance struct {
   name    string
   version string
}

func (instance *EntityInstance) Init() {
   //做一些初始化操作
}

主方法位于不同的代码库/应用程序中,并使用Instance应用程序来调用某些初始化操作。
目前,编写上述主方法的用户需要显式调用Instance.Init()

目标是让开发人员(在这种情况下是实现主方法的人)只关注他们自定义的任何初始化操作,而不必担心调用Instance.Init()OnInit()调用应该隐式地处理Instance.Init()

有什么帮助可以让我朝正确的方向开始?

英文:

I'm trying to create a template method that should be executed a certain other method is called.
For example:

func main(){
   onInit()
}

func onInit(){
   var Instance entity.EntityInstance
   Instance.Init()
   //do something
}

Another source file, instance.go

type EntityInstance struct{
   name string
   version string
}
func (instance *EntityInstance) Init(){
// do some initialization
}

The main method is in different code base/app and uses the Instance app to invoke certain initializations.
Currently the user writing this above main method needs to explicitly call the Instance.init()

The objective is for the developers (in this case one who implements the main method) only concern themselves with any of their custom initializations and not worry about calling "Instance.Init()". The OnInit() invoke should take care of "Instance.Init()" implicitly.

Any help to get me started in the right direction ?

EDIT: I do understand that the exact OOP concepts cannot be translated here in Golang but all I'm looking for is the appropriate approach. Clearly, I need to change the way I think of design in here but just don't know how.

答案1

得分: 1

你的问题有点不清楚,我怀疑是因为你试图直接从另一种语言中翻译思想和习语,你应该避免这样做。然而,如果你想要在Go中使用隐式的包初始化,你可以使用魔术函数名

func init(){}

https://golang.org/doc/effective_go.html#init

每个源文件都可以定义自己的无参数init函数来设置所需的状态。(实际上,每个文件可以有多个init函数。)"finally"的意思是最后:init函数在包中的所有变量声明都已经评估其初始化器之后调用,而这些初始化器只有在所有导入的包都被初始化之后才会被评估。

不过要小心使用,这是隐式行为,如果调用者在导入你的包时不知道这一点,可能会导致神秘的错误。

英文:

Your question is a little unclear, I suspect because you are trying to directly translate ideas and idioms from another language, you should resist doing that. However, if you want an implicit init for a package in Go, you can use the magic function name

func init(){}

https://golang.org/doc/effective_go.html#init

> Finally, each source file can define its own niladic init function to
> set up whatever state is required. (Actually each file can have
> multiple init functions.) And finally means finally: init is called
> after all the variable declarations in the package have evaluated
> their initializers, and those are evaluated only after all the
> imported packages have been initialized.

Be careful with this though, it is implicit behaviour and could cause mysterious bugs if your callers don't know it is happening when they import your package.

huangapple
  • 本文由 发表于 2017年8月23日 02:13:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/45824379.html
匿名

发表评论

匿名网友

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

确定