Go modules layered dependencies

Following the package structure

go_experiments
  -module_1
    -package_1
      -package_1.go
    -package_2
      -package_2.go
    -sample_file_1.go //Q1: Why is this file mandatory? I was getting error without this file
    -go.mod

  -module_2
    -go.mod                //requires module_1
    -sample_file_2.go //this file calls method which are located inside package_2.go file

  -consume
    -go.mod  // Q2: Why do I have to 'require' module_1 and module_2 ? I should just require module_2 and this package doesnt care what module_2 requires, the dependencies should be resolved automatically
    -main.go //This calls method which is located in sample_file_2

Question1: Why is the file sample_file_1.go mandatory? I was getting error without this file?
Question 2: The consume module calls sample_file_2.go which is located in module_2.Therefore it just needs to require module_2 in the go.mod. However I find out its giving error and the error is resolved once I include module_1 as well. Therefore the question is how to deal with this situation? It cannot be expected for every child module to include/require its parent module—>grand module---->grand grand modules —> and higher up the hierarchy. Is mentioning the lineage necessary in the go.mod file or I am missing something?

It seems as if you missed to add your question…

1 Like

@NobbZ I have added the questions on the edit

1 Like

But none of the errors you are running into.

1 Like

In your module 2, you should requires module_1/package_2 instead of the module_1 root directory. Go treats every directory as a package.

Since you do not have any source codes in the root directory, hence to “no go source codes” appears.

This concept was used for major version release 2 and more, e.g. github.com/me/mygopackage/v2.

You do need to require every grand parents. The problem was due to module_2 cannot resolve its dependencies.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.