Example of go module methods using local packages
- 2020-07-21 08:21:50
- OfStack
go module is very simple to use
Initialize go mod
go mod init
Collate dependent packages
go mod tidy
If you want to cache to the vendor directory
go mod vendor
The dependencies are automatically fixed after the command is executed.
However, if we are a locally developed package, how do we solve the local package dependency problem when there is no remote repository?
Use replace to replace remote packages with local package services
Fortunately, go module offers another option, replace. How does this replace work?
Let's start with a basic mod file
module GoRoomDemo
go 1.12
require (
github.com/gin-gonic/gin v1.3.0
github.com/gohouse/goroom v0.0.0-20190327052827-9ab674039336
github.com/golang/protobuf v1.3.1 // indirect
github.com/gomodule/redigo v2.0.0+incompatible
github.com/mattn/go-sqlite3 v1.10.0
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 // indirect
)
This is a dependency package for a simple GoRoom framework. If I want to use the native goroom, I just need to use replace
module GoRoomDemo
go 1.12
require (
github.com/gin-gonic/gin v1.3.0
github.com/gohouse/goroom v0.0.0-20190327052827-9ab674039336
github.com/golang/protobuf v1.3.1 // indirect
github.com/gomodule/redigo v2.0.0+incompatible
github.com/mattn/go-sqlite3 v1.10.0
github.com/stretchr/testify v1.3.0 // indirect
golang.org/x/net v0.0.0-20190320064053-1272bf9dcd53 // indirect
)
replace github.com/gohouse/goroom => /path/to/go/src/github.com/gohouse/goroom
Here,
path/to/go/src/github.com/gohouse/goroom
Is the local packet path
This way, we can happily use the local directory