如何开发和测试本地go模块 [英] How to develop and test a local go module
问题描述
我有这个脚本:
#!/usr/bin/env bash
set -eo pipefail
cd "$(dirname "$BASH_SOURCE")";
pth_to_make="$GOPATH/src/ores/json-logging"
mkdir -p "$pth_to_make";
rm -rf "$pth_to_make";
ln -sf "$PWD" "$pth_to_make";
但是在go.mod中没有将该模块声明为go模块...我正在将另一个依赖项符号链接到仓库中以对其进行测试.
however this module is not declared as a go module in go.mod...I am symlinking a different dependency into the repo to test it.
生产中的真正依存关系是:
The real dependency in production is at:
"$GOPATH/src/github.com/oresoftware/json-logging"
测试路径为:
"$GOPATH/src/ores/json-logging"
现在,我只想删除本地开发中的真实依赖关系路径,问题是它只删除了仓库中的.git文件夹,然后我不得不再次下载它.
Now, I would just delete the real dependency path in local development, the problem is that it then just deletes the .git folder in the repo and then I end up having to download that again.
我是否可以通过某种方式对真正的repo/dep文件夹进行符号链接,但又不会丢失git的东西?也许我可以做:
Is there some way for me to symlink over the real repo/dep folder, but not lose the git stuff? Maybe I could do:
mv "$GOPATH/src/github.com/oresoftware/json-logging" "/tmp/gotemp/json-logging"
# call the script above
# then when I am done:
rm -rf "$GOPATH/src/github.com/oresoftware/json-logging"
mv "/tmp/gotemp/json-logging" "$GOPATH/src/github.com/oresoftware/json-logging" # put it back
那是最好的前进方式吗?
would that be the best way forward?
推荐答案
因此,在我的评论中,听起来好像您所追求的是 replace
指令,您可以阅读有关此处.但我在下面概述了一个简单的例子;
So to expand on my comment it sounds like what you are after is the replace
directive which you can read more about here. But i've outlined a brief example below;
如果我们想象一下目录结构:
If we imagine the directory structure:
├── replace-directive
│ ├── go.mod
│ └── replace.go
├── sandbox
│ ├── go.mod
│ └── main.go
replace-directive
是一个go模块,其中的 go.mod
文件包含:
The replace-directive
is a go module with the go.mod
file containing:
module example.com/replacedirective
go 1.14
沙盒是我们要使用此本地依赖版本的地方,而不是从 example.com
抓取的版本.因此,在我们的沙盒 go.mod
文件中,我们具有如下所示的replace指令;
Sandbox is where we want to use this local dependency version rather than grabbing the version from example.com
. So in our sandbox go.mod
file we have the replace directive as follows;
module example/sandbox
require (
example.com/replacedirective v0.0.0 // This would be your reference to your production dependency
)
replace example.com/replacedirective => ../replace-directive // This is your local dependency
go 1.14
因此,现在当您在代码中引用模块时,它将使用本地依赖项代替.无需符号链接.
So now when you reference the module in your code it will use your local dependency instead. No symlinks required.
这篇关于如何开发和测试本地go模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!