如何开发和测试本地go模块

时间:2020-03-12 06:04:57

标签: git go symlink go-modules

我有这个脚本:

#!/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模块...我正在将另一个依赖项符号链接到仓库中以对其进行测试。

生产中的真正依赖性在于:

"$GOPATH/src/github.com/oresoftware/json-logging"

测试路径为:

"$GOPATH/src/ores/json-logging"

现在,我只删除本地开发中的真实依赖关系路径,问题是它只删除了仓库中的.git文件夹,然后我不得不再次下载它。

我是否可以通过某种方式对真正的repo / dep文件夹进行符号链接,但又不会丢失git的东西?也许我可以做:

 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

那是最好的前进方式吗?

1 个答案:

答案 0 :(得分:1)

因此,我想听听我的评论是replace指令,您可以阅读有关here的更多信息。但我在下面概述了一个简短的示例;

如果我们想象一下目录结构:

├── replace-directive
│   ├── go.mod
│   └── replace.go
├── sandbox
│   ├── go.mod
│   └── main.go

replace-directive是一个go模块,其中的go.mod文件包含:

module example.com/replacedirective

go 1.14

沙盒是我们要使用此本地依赖项版本的地方,而不是从example.com获取该版本的地方。因此,在我们的沙盒go.mod文件中,我们具有如下所示的replace指令;

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

因此,现在当您在代码中引用模块时,它将使用本地依赖项代替。无需符号链接。