Let me define this first modules
are collections of packages
. In Go 11, I use go modules like the following:
If both packages are in the same project, you could just do the following:
In go.mod
:
module github.com/userName/moduleName
and inside your main.go
import "github.com/userName/moduleName/platform"
However, if they are separate modules, i.e different physical paths and you still want to import local packages
without publishing this remotely to github for example, you could achieve this by using replace
directive.
Given the module name github.com/otherModule
and platform
, as you've called it, is the only package inside there. In your main module's go.mod
add the following lines:
module github.com/userName/mainModule
require "github.com/userName/otherModule" v0.0.0
replace "github.com/userName/otherModule" v0.0.0 => "local physical path to the otherModule"
Note: The path should point to the root directory of the module, and can be absolute or relative.
Inside main.go
, to import a specific package like platform
from otherModule
:
import "github.com/userName/otherModule/platform"
Here's a gentle introduction to Golang Modules
The easiest and working out-of-the-box solution is to put your database
package / module into a VCS (e.g. github.com), so other packages (inside other modules) can simply refer to it by importing it like:
import "github.com/someone/database"
If you do so, you don't even have to fiddle with the go.mod
files manually, everything will be taken care of by the go tool: it will automatically recognize and resolve this dependency, download and install the required package, and will also update go.mod
automatically.
Staying entirely on local disk
If you don't want to use a VCS (e.g. you're just experimenting or you haven't decided what to use yet), then you can still do it. The how is detailed in the official Go Wiki: Can I work entirely outside of VCS on my local filesystem?
So you created a database
folder outside of GOPATH
, and you created a module in it. And you created another module, let's call it main
, and you want to use this database
package.
What you must do is:
go.mod
of your main
module must list the database
package as a "requirement". Give a temporary VCS name to your database
package:
require (
example.com/me/database v0.0.0
)
You must tell the go tool where this package is located, because the full package name we used is just a temporary / fantasy name. Use the replace
directive to make this database
package point to a folder on your local disk; you may use absolute and relative paths:
replace example.com/me/database => ../database
And that's all.
Working example
Let's see a working example. Let's create a pretty
module. Create a pretty
folder with 2 files in it:
pretty.go:
package pretty
import "fmt"
func Pretty(v ...interface{}) {
fmt.Println(v...)
}
go.mod (can be created by running go mod init pretty
):
module pretty
Now let's create another, main module. Let's create a folder osinf
(it may be whatever) next to the pretty
folder. 2 files in it:
osinf.go (note we intend to use our pretty
package / module, we import it by "example.com/me/pretty"
):
package main
import "example.com/me/pretty"
func main() {
pretty.Pretty("hi")
pretty.Pretty([]int{1, 3, 5})
}
go.mod:
module main
require example.com/me/pretty v0.0.0
replace example.com/me/pretty => ../pretty
And that's all.
Running go run osinf.go
in the osinf
folder, the output is:
hi
[1 3 5]
Best Answer
So you wrote a Go "library" module which:
Given the module name "X" as you've called it, in your main module add the following lines:
The path should point to the root directory of the module, and can be absolute or relative.
To import package util from module X:
(You don't import modules. You import packages from modules.)
Explanation
Go's module functionality is designed for publicly published modules. Normally, a module's name is both its unique identifier and the path to its public repo. When your go.mod declares a module dependency with the
require
directive, Go will automatically find and retrieve the specified version of the module at that path.(See also in the Go Modules FAQ, Can I work entirely outside of VCS on my local filesystem?)
If, for example, your
go.mod
file containsrequire github.com/some/dependency v1.2.3
, Go will retrieve the module from GitHub at that path. But if it containsrequire X v0.0.0
, "X" isn't an actual path and you will get the errorcannot find module for path X
.The
replace
directive allows you to specify a replacement path for a given module identifier and version. There are many reasons you'd want to do this, such as to test changes to a module before pushing them to the public repo. But you can also use it to bind a module identifier to local code you don't ever intend to publish.Hope this helps.