Since the introduction of modules in Go 1.11, the way modules are referenced and structured has changed significantly. To reference a module from a different directory, the following steps should be taken:
Old Way:
New Way:
Let's consider the following project structure:
\root \module1 \go.mod \module2 \go.mod
To reference module2 from module1, follow these steps:
Use a relative import path in module1 source code:
import "./../module2"
In the new module system, Go supports relative importing, allowing developers to reference modules relative to the current module's location. The syntax for relative importing is:
import "./relative/path/to/module"
This approach enables the separation of projects into modules while maintaining easy referencing between modules.
The above is the detailed content of How to Effectively Structure and Reference Golang Modules in Different Directories?. For more information, please follow other related articles on the PHP Chinese website!