Go: require module from local folder with replace

huangapple go评论76阅读模式
英文:

Go: require module from local folder with replace

问题

我有:

  • 一个模块 (/Users/collimarco/Sites/pushpad-go)
  • 一个使用该模块的演示应用程序 (/Users/collimarco/Sites/pushpad-example-app/go)

它们位于两个不同的文件夹中,路径也不同。

我需要遵循这个结构,因为许多其他语言也使用相同的约定。

这是模块的内容:

// /Users/collimarco/Sites/pushpad-go/go.mod

module github.com/pushpad/pushpad-go

go 1.19

这是演示应用程序,它需要使用该模块:

// /Users/collimarco/Sites/pushpad-example-app/go/go.mod

module pushpad-go-example

go 1.19

replace "github.com/pushpad/pushpad-go" v0.0.0 => "/Users/collimarco/Sites/pushpad-go"

// /Users/collimarco/Sites/pushpad-example-app/go/main.go

package main

import (
  "github.com/pushpad/pushpad-go/pushpad"
)

func main() {
  n := pushpad.Notification { Body: "Example notification" }
  n.Broadcast()
}

我阅读了不同的博文,这似乎是正确的方法。然而,我遇到了这个错误:

$ go run .
main.go:4:3: no required module provides package github.com/pushpad/pushpad-go/pushpad; to add it:
	go get github.com/pushpad/pushpad-go/pushpad

我不想从在线仓库下载该模块,就像错误提示的那样:我只想从本地文件夹导入代码。我应该怎么做?

英文:

I have:

  • a module (/Users/collimarco/Sites/pushpad-go)
  • a demo application that uses that module (/Users/collimarco/Sites/pushpad-example-app/go)

They are in two separate folders, in different paths.

I need to follow this structure, because the same convention is used for many other languages.

This is the module:

// /Users/collimarco/Sites/pushpad-go/go.mod

module github.com/pushpad/pushpad-go

go 1.19

This is the demo application that needs to use that module:

// /Users/collimarco/Sites/pushpad-example-app/go/go.mod

module pushpad-go-example

go 1.19

replace "github.com/pushpad/pushpad-go" v0.0.0 => "/Users/collimarco/Sites/pushpad-go"

// /Users/collimarco/Sites/pushpad-example-app/go/main.go

package main

import (
  "github.com/pushpad/pushpad-go/pushpad"
)

func main() {
  n := pushpad.Notification { Body: "Example notification" }
  n.Broadcast()
}

I read different blog posts and that seems the correct approach. However I get this error:

$ go run .
main.go:4:3: no required module provides package github.com/pushpad/pushpad-go/pushpad; to add it:
	go get github.com/pushpad/pushpad-go/pushpad

I don't want to download the module from an online repository, as the error suggests: I just want to import the code from the local folder. What should I do?

答案1

得分: 2

这里的第一个问题是你导入了github.com/pushpad/pushpad-go/pushpad;末尾的/pushpad是个问题,因为它不是仓库路径的一部分。所以你的main.go中的代码应该是这样的:

import (
  "github.com/pushpad/pushpad-go"
)

你还需要为模块添加一个require指令。如果你使用go get命令获取它的版本为v0.0.0,replace指令将会从你的本地文件夹中检索它。所以:

go get github.com/pushpad/pushpad-go@0.0.0

应该将以下内容添加到你的go.mod文件中:

require (
    github.com/pushpad/pushpad-go v0.0.0
)

另外,在你的replace指令中不需要引号。应该是这样的:

replace github.com/pushpad/pushpad-go v0.0.0 => /Users/collimarco/Sites/pushpad-go

另一种做法是,而不是手动编辑你的go.mod文件,可以使用go mod edit -replace命令,像这样:

go mod edit -replace github.com/pushpad/pushpad-go=/Users/collimarco/Sites/pushpad-go

这样应该会"做正确的事情"。

英文:

The first problem here is that you're importing github.com/pushpad/pushpad-go/pushpad; the trailing /pushpad is a problem because that is not part of the repository path. So the code in your main.go should have:

import (
  "github.com/pushpad/pushpad-go"
)

You still need a require directive for the module as well. If you go get it at version v0.0.0, the replace directive will retrieve it from your local folder. So:

go get github.com/pushpad/pushpad-go@0.0.0

should add this to your go.mod:

require (
    github.com/pushpad/pushpad-go v0.0.0
)

Also, quotes are not necessary in your replace directive. It should be:

replace github.com/pushpad/pushpad-go v0.0.0 => /Users/collimarco/Sites/pushpad-go

Another way of doing this, rather than editing your go.mod file manually, is to use go mod edit -replace like so:

go mod edit -replace github.com/pushpad/pushpad-go=/Users/collimarco/Sites/pushpad-go

That should "do the right thing".

huangapple
  • 本文由 发表于 2022年12月23日 00:15:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/74891171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定