英文:
Go project containing Typescript AWS-CDK directory has cluttered go.mod file
问题
我有一个包含Typescript AWS-CDK目录的Go项目,用于定义包含Go二进制文件的堆栈。
这个示例的最简项目是:
mkdir example
cd example
mkdir infrastructure
cd infrastructure
cdk init app --language typescript
cd ..
go mod init example
go mod tidy
到这一步,go.mod
中充满了aws-cdk-go
的引用(尽管我使用的是Typescript)。理想情况下,此时go.mod
不应该有任何依赖项。看起来,在node_modules/aws-cdk/....
路径中有几个Go文件可能会影响go mod tidy
。
我还不打算使用Go CDK库。除了将所有的Go代码放入一个子文件夹中,并只在该子文件夹中运行go mod init
之外,是否有办法阻止go mod tidy
引用那些被隐藏的文件?
英文:
I have a Go project containing a Typescript AWS-CDK directory to define the Stack that contains the Go binary.
The simplest project for this example is:
mkdir example
cd example
mkdir infrastructure
cd infrastructure
cdk init app --language typescript
cd ..
go mod init example
go mod tidy
At this point, go.mod
is full of aws-cdk-go
references (despite the fact I'm using Typescript). Ideally, go.mod
shouldn't have any dependencies at this point. It appears that there are several Go files buried in the node_modules/aws-cdk/....
path that may be influencing go mod tidy
.
I'm not yet interested in using the Go CDK libraries. Besides throwing all of my Go code into a subfolder and only running go mod init
in that subfolder, is there a way to prevent go mod tidy
from referencing those buried files?
答案1
得分: 1
在infrastructure
目录中,使用touch go.mod
命令创建一个空文件,如果我在根目录中运行go mod tidy
命令,go.mod
文件将只包含我项目实际使用的模块。
英文:
> An empty go.mod in a directory will cause that directory and all of
> its subdirectories to be excluded from the top-level Go module.
In the infrastructure
directory, touch go.mod
creates an empty file and if I go mod tidy
in the root folder, the go.mod
file only contains the modules my project actually uses.
答案2
得分: 0
go mod init
查找配置文件,如果找到,则安装模块。
通过在子目录中运行 go mod init
[Edit] <s>before cdk init app</s>,可以避免这种“有帮助”的行为:
# ...
npx cdk init app --language typescript
cd ..
mkdir src
cd src
go mod init example
go mod tidy
cd ..
这将创建一个预期的空的 go.mod
文件。
英文:
go mod init
looks for configuration files and, if found, installs the modules.
Avoid this "helpful" behaviour by running go mod init
[Edit] <s>before cdk init app</s> in a subdirectory:
# ...
npx cdk init app --language typescript
cd ..
mkdir src
cd src
go mod init example
go mod tidy
cd ..
This creates an empty go.mod
, as expected.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论