How to use an alternate go.mod file for local development?

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

How to use an alternate go.mod file for local development?

问题

目前我正在开发一个使用Serverless Framework和Go语言的API。

我正在使用Serverless-offline插件进行本地测试。

这个API依赖于其他几个仓库(我也在维护),我使用go.mod文件进行导入。

然而,我在完善我的开发工作流程方面遇到了一些困难。

目前,如果我想对这个API所依赖的仓库进行更改,我必须修改项目的go.mod文件,以便进行测试目的的替换指令,但是在部署到生产环境之前,我又必须手动改回来。

基本上,我正在寻找一种只在本地开发时应用替换指令的方法。其他人是如何解决这个问题的?

附加问题:有没有办法在Docker中运行Serverless offline?我发现在裸机上运行serverless-offline会导致不同开发环境之间的不一致性。

英文:

Currently I am working on an API which uses Serverless Framework with Go.

I'm using the Serverless-offline plugin for local testing.

This API depends on a few other repositories (which I also maintain), which I import using the go.mod file.

However I am having a hard time refining my developer workflow.

Currently, if I want to make changes in a repository which this API depends upon, I have to alter the projects go.mod to include replace directives for the purpose of testing, but then I'm having to manually change it back for deployment to production.

Basically I'm looking for a way to include replace directives, which only get applied during local development. How has everyone else dealt with this problem?

Bonus question: Is there any way to run Serverless offline in docker? I'm finding that serverless-offline running on the bare metal is causing inconsistencies between different developers environments.

答案1

得分: 7

你可以使用-modfile选项来运行带有替代go.mod文件的go命令:

来自构建命令

-modfile=file.mod标志指示go命令读取(可能还会写入)一个替代文件,而不是模块根目录中的go.mod。该文件的名称必须以.mod结尾。必须仍然存在名为go.mod的文件,以确定模块根目录,但不会访问它。当指定了-modfile时,还会使用替代的go.sum文件:其路径是通过修剪-modfile标志的.mod扩展名并追加.sum来派生的。

为开发和构建创建一个带有必要的replace指令的local.go.mod文件,例如:

go build -modfile=local.go.mod ./...
英文:

You can run go commands with an alternate go.mod file with the -modfile option:

From Build commands:

> The -modfile=file.mod flag instructs the go command to read (and
> possibly write) an alternate file instead of go.mod in the module root
> directory. The file’s name must end with .mod. A file named go.mod
> must still be present in order to determine the module root directory,
> but it is not accessed. When -modfile is specified, an alternate
> go.sum file is also used: its path is derived from the -modfile flag
> by trimming the .mod extension and appending .sum.

Create a local.go.mod file with the necessary replace directive for development and build, for example, with:

go build -modfile=local.go.mod ./...

答案2

得分: 1

在Go 1.18或更高版本中,最简单的方法是使用新的Go工作区功能。请参阅以下链接:

基本上,使用go work init命令创建一个go.work文件,该文件可以包含局部替换指令,超出了go.mod的范围。

英文:

The easiest way to do this in Go 1.18 or later is using the new Go workspaces feature. See

Essentially, use go work init to create a go.work file, that can contain local replacement directives, outside the scope of your go.mod.

huangapple
  • 本文由 发表于 2021年8月13日 06:02:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/68764637.html
匿名

发表评论

匿名网友

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

确定