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

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.


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.