Noob Question : complex hello world example

Hello ,
Sorry for such a simple question :

I am looking for a complex hello world example to illustrate a general module architecture and illustrate the maximum of situations :

  • 4 packages , including packages at the same level and sub-packages
  • 3 executables applications
    appli_1.exe
    appli_2.exe
    appli_3.exe

The purpose of my question is to clearly understand the state of the art of import declaration in all the standard cases :
Q1) SUB_BRANCH ACCESS : import for accessing from Hello_from_a a function in sub_package_a_aa or sub_package_a_bb

Q2) EXTRA BRANCH ACCESS : import from sub_package_a_bb and call a function in sub_package_b (in another branch)

Q3) organization for many executables
I tried ./cmd/appli_1/ appli_1.go
./cmd/appli_2/ appli_2.go
./cmd/appli_3/ appli_3.go

but I have difficulties to call ./package_a/sub_package_a_aa functions
from ./cmd/appli_2/ appli_2.go main executable

Structure ?:
./ cmd / appli_1 / appli1.go
./ cmd / appli_2 / appli2.go
./ cmd / appli_3 / appli3.go

. / package_a
file : package_a.go with for example a function Hello_from_a()

. / package_a / sub_package_a_aa (example for sub_branch)
file : package_a_aa.go with for example a function Hello_from_a_aa()

. / package_a / sub_package_a_bb (example2 for sub_branch)
file : package_a_bb.go with for example a function Hello_from_a_bb()

. / package_b / (example for multiple branches)
file : package_b with for example a function Hello_from_b()

Hi, Go has NOT official project structure but recently the official doc got this section

In your case (mutiple executable and several packages, in short:

  • for the executables you can use the pattern cmd/<exe-name>/main.go
  • for packages:
    • for “internal only packages” you should use internal/<package-name>/*.go
    • for packages meant to be imported from external packages/apps you can use ./<package-name>/*.go

Keep in mind the the import scope starts from the module root directory so if you have symbols defined in internal/mypackage/*.go you can use then just importing

import "<module-name>/internal/mypackage"
1 Like

Start from the folder structure, here is how I would organize: Assuming 4 packages belong to the same project or module named project1, place each package in its own folder, and under the parent folder project1/
In each of the package folders, if the code need to access other folder’s data, import it by providing the path starting with project1, such as project1/package1. Then you can reference the function using dot notation as package1.dosomthing()
Hope this helps!