go module is the official version management tool introduced after Go 1.11, and since Go 1.13, go module has been the default dependency management tool for the Go language. Since Go 1.14, the go modules feature has been officially recommended for use in production environments.
Here is a detailed description of how to use the go module to import local packages.
Prerequisites
Suppose we now have two packages, moduledemo and mypackage, where the moduledemo package will import the mypackage package and use its New method.
mypackage/mypackage.go reads as follows.
We now discuss this in two scenarios.
Under the same project
Note: We are allowed to define multiple packages under a project
Catalog Structure
What happens now is that we call the package mypackage in moduledemo/main.go.
Import Package
At this point, we need to define it in moduledemo/go.mod as follows.
Then import the mypackage in moduledemo/main.go as follows
As an example
Suppose we now have the following file directory structure.
where bubble/go.mod reads as follows.
bubble/dao/mysql.go reads as follows.
bubble/main.go reads as follows.
Not under the same project
Catalog Structure
Import Package
At this point, mypackage also needs to be initialized with a module, i.e., have a go.mod file of its own, with the following content.
Then we import in moduledemo/main.go as follows.
Because the two packages are not in the same project path, you want to import the local packages and they are not published to a remote github or other code repository address. At this point we need to use the replace directive in the go.mod file.
In the caller, which is moduledemo/go.mod, specify the relative path to use to find the package mypackage as follows.
As an example
Finally, let’s consolidate the above with an example.
We now have the following file directory structure.
The function defined in p1/main.go that you want to import in p2.go.
The contents of p2/go.mod are as follows.
import in p1/main.go as follows
Because I didn’t upload the package www.sobyte.netq1mi/p2 to the site www.sobyte.net, we just want to import the local package, and this time we need to use the replace command.
p1/go.mod reads as follows.
At this point, we can compile the p1 project normally.
Reference https://www.liwenzhou.com/posts/Go/import_local_package_in_go_module/