有没有一种有效的方法可以在Go语言包之间共享结构?

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

Is there an efficient way to share structure between golang packages?

问题

我有一个用Go编写的简短程序,其中包含以下文件的一部分。
程序目录结构:

myprogram/
    main.go
    server.go
    routines.go
    structs.go

这些不同的文件包含不同的函数。structs.go文件包含一系列在程序的多个文件中定义和使用的结构类型。现在我想要做的是将我的程序拆分成包,就像这个例子一样:

main/
    main.go // 主程序
server/
    server.go // 在主程序中导入的包
routines/
    routines.go // 在主程序中导入的包

我的问题是,我不知道应该把structs.go放在哪里,因为它包含在多个包中使用的结构,就像main.go代码中一样。

如何有效地共享这个structs.go文件?我应该将它(通过符号链接)包含在我定义的每个包中,即serverroutines,还有主程序中吗?

我的方法可能很笨拙,因为我是Go的初学者,也是编程的初学者。

英文:

I have a short program in Go with the following files part of it.
Program directory structure:

myprogram/
    main.go
    server.go
    routines.go
    structs.go

These different files contain different function. The structs.go file contains a list of structure type defined, and used in several files of my program. What I want to do, now is to split my program into package like in this example :

main/
    main.go // the main program
server/
    server.go // a package imported in main
routines/
    routines.go // a package imported in main

My problem, is that I do not know where to put structs.go because it contains structures used in several package, as in the 'main.go' code.

How to share efficiently this structs.go file ? Should I include it (via a symlink to the file) in each of the package I defined, i.e serverand routines and also in main ?

My method may be awkward because I'm a beginner in Go, and in programming generally.

答案1

得分: 20

不要在不同的包之间链接文件,这是不好的实践。首先,代码会被复制。其次,标识符会被复制,意味着表示相同实体(例如类型或函数)的标识符将是不同的。例如,如果链接并且structs.go包含一个type Response的定义,你将会有两个不同的类型server.Responseroutines.Response,这只会增加混淆。

一种解决方案是将structs.go放入自己的包中,例如model,并且所有依赖它的其他包可以导入它(例如你的mainserverroutines)。

在一个理论上的例子中:如果包A导入包B,并且structs.go在两个包中都需要,那么它也可以添加到包B中。如果有一个只需要structs.go的包C,那么再次创建一个自己的包model会更明智(这样包C不需要导入/了解包B,只需要新的model包)。

另外,如果没有其他人会使用你的包,并且它不太复杂,将其组织成多个包可能不值得麻烦。

英文:

Don't link files across packages, that's bad practice. For one, the code will be duplicated. For another, identifiers will be duplicated meaning to denote the same entities (e.g. type or function), but they will be distinct. E.g. if linked and structs.go would contain a type Response definition, you would have 2 distinct types server.Response and routines.Response giving just more confusion.

One solution would be to put structs.go into its own package, e.g. model, and all other packages relying on it can import it (e.g. your main, server and routines).

In a theoretical example: if package A imports package B and structs.go would be needed in both, then it could also be added to package B. If there would be a package C needing only structs.go, then again it would be wiser to create its own package model (so package C doesn't need to import / know about package B, only the new model package).

Also if noone else will use your package and it is not too complex, it might not worth the hassle to organize it into multiple packages.

答案2

得分: 1

可以在一个包中定义一个类型,并在其他包中使用它,有两种方式:

变体1:

package two
import "one"
var name one.A

变体2:

package two
import "one"
type A = one.A
var name A

你更喜欢变体2。

英文:

It is possible to define a type in one package only and to use it in other packages this way:

package one
type A struct{ B int }

Variant 1:

package two
. import "one"
var name A

Variant 2:

package two
import "one"
type A = one.A
var name A

I would prefer variant 2.

huangapple
  • 本文由 发表于 2016年2月23日 19:28:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/35576337.html
匿名

发表评论

匿名网友

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

确定