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

ElieLie

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.

icza

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Most efficient way to share data between a Service and an Activity

From Dev

Share structure between GraphQL schemas

From Dev

Meteor 1.2 share files between packages

From Dev

Efficient way to structure falcor router for deep paths?

From Dev

Efficient way to structure falcor router for deep paths?

From Dev

How does Golang share variables between goroutines?

From Dev

Efficient way to match regex between delimiters

From Dev

Efficient way to get location of match between vectors

From Dev

Efficient way to find the difference between 2 IEnumerables

From Dev

Finding a string between strings in efficient way

From Java

Correct way to share functions between components in React

From Java

Is there a way to share a configMap in kubernetes between namespaces?

From Dev

Best way to share object between threads?

From Dev

Best way to share object between threads?

From Dev

How to share private meteor packages between multiple projects?

From Dev

Is there a way to differentiate between System packages and Manually installed packages in Linux?

From Dev

How to create structure in MATLAB with many repeated substructures in an efficient way?

From Dev

The efficient way to make external data structure updater UI

From Dev

What would be the most efficient way to structure a VueJS app with this logic?

From Dev

casting a pointer to an array to a structure in efficient C++11 way

From Dev

Is there a way to add a submodule to a git repo that share a directory structure

From Dev

Is there a way to add a submodule to a git repo that share a directory structure

From Dev

Most efficient way to read Zlib compressed file in Golang?

From Dev

How to share records structure between projects, with slight variations?

From Dev

Is there a standard way to keep a database session open across packages in Golang?

From Dev

How to import packages between themselves under one repository in golang?

From Dev

What is the most efficient way of sending files between NodeJS servers?

From Dev

Dot Product between 2 matrices in ruby, most efficient way

From Dev

Gremlin: What's an efficient way of finding an edge between two vertices?

Related Related

  1. 1

    Most efficient way to share data between a Service and an Activity

  2. 2

    Share structure between GraphQL schemas

  3. 3

    Meteor 1.2 share files between packages

  4. 4

    Efficient way to structure falcor router for deep paths?

  5. 5

    Efficient way to structure falcor router for deep paths?

  6. 6

    How does Golang share variables between goroutines?

  7. 7

    Efficient way to match regex between delimiters

  8. 8

    Efficient way to get location of match between vectors

  9. 9

    Efficient way to find the difference between 2 IEnumerables

  10. 10

    Finding a string between strings in efficient way

  11. 11

    Correct way to share functions between components in React

  12. 12

    Is there a way to share a configMap in kubernetes between namespaces?

  13. 13

    Best way to share object between threads?

  14. 14

    Best way to share object between threads?

  15. 15

    How to share private meteor packages between multiple projects?

  16. 16

    Is there a way to differentiate between System packages and Manually installed packages in Linux?

  17. 17

    How to create structure in MATLAB with many repeated substructures in an efficient way?

  18. 18

    The efficient way to make external data structure updater UI

  19. 19

    What would be the most efficient way to structure a VueJS app with this logic?

  20. 20

    casting a pointer to an array to a structure in efficient C++11 way

  21. 21

    Is there a way to add a submodule to a git repo that share a directory structure

  22. 22

    Is there a way to add a submodule to a git repo that share a directory structure

  23. 23

    Most efficient way to read Zlib compressed file in Golang?

  24. 24

    How to share records structure between projects, with slight variations?

  25. 25

    Is there a standard way to keep a database session open across packages in Golang?

  26. 26

    How to import packages between themselves under one repository in golang?

  27. 27

    What is the most efficient way of sending files between NodeJS servers?

  28. 28

    Dot Product between 2 matrices in ruby, most efficient way

  29. 29

    Gremlin: What's an efficient way of finding an edge between two vertices?

HotTag

Archive