Import local package that's already in public repo

廉价感情. 提交于 2021-01-20 12:40:32

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!