问题
I am working on building a web framework in Go. I have the code locally and would like to use that repo in another module to test that everything works without having to create tags and/or push things to remote repos. I have followed the official docs on how to do this, as well as a number of posts elsewhere. However nothing seems to work. What am I doing incorrectly?
Package live here locally:
../goworkspace/src/github.com/garrettlove8/goserve
Import from other module:
...
import (
"fmt"
"io"
"net/http"
"github.com/garrettlove8/goserve" // I have tried "goserve" and "../goserve"
)
...
go.mod:
...
require github.com/garrettlove8/goserve v0.1.17
No matter what I do it doesn't seem to work as desired
Update
Code and error combos:
// main.go
import (
"fmt"
"io"
"net/http"
"goserve"
)
Run go mod tidy
// go.mod becomes
require github.com/garrettlove8/goserve v0.1.17 // indirect
Error:
goserve: package goserve is not in GOROOT (/usr/local/go/src/goserve)
Manually changing go mod to this (which I don't to have to do):
require github.com/garrettlove8/goserve
Run go mod tidy
Error:
usage: require module/path v1.2.3
回答1:
When you want to use a local module instead of a remote one, you can achieve that with the replace directive.
In your case, add this to your go.mod
file:
replace github.com/garrettlove8/goserve => ../goworkspace/src/github.com/garrettlove8/goserve
来源:https://stackoverflow.com/questions/64295033/import-local-package-thats-already-in-public-repo