英文:
Run a single Go file with a single local import without using modules
问题
我有一系列的go文件,它们通过使用关联在一起,但在逻辑上是独立的。它们都使用一个单独文件中定义的共同的辅助函数集。
我的目录结构如下所示。
src/
├── foo1.go
├── foo2.go
├── ...
├── fooN.go
└── helper/
└── helper.go
foox.go文件的格式如下 -
package main
import help "./helper"
// 使用helper中的功能,但与其他foox.go文件中发生的任何事情都无关的函数和结构体
func main() {
// 使用helper中的功能,但与其他foox.go文件中发生的任何事情都无关的更多内容
return
}
我之前是使用go run foox.go
来运行特定的文件,但最近更新了我的Go版本。由于不再支持相对导入,这个工作流程现在无法正常工作 -
"./helper"是相对路径,但在模块模式下不支持相对导入路径
在这种情况下,正确的方式是如何组织一组相互独立的Go文件,它们都依赖于相同的辅助函数集?
所有的指导都说要使用模块,但在这种情况下,这意味着每个foox.go文件都需要一个单独的模块,其中每个文件包含的func
、struct
等将永远不会在任何其他模块中使用。
我只想运行一个包含另一个本地.go文件的单个.go文件,而不必费力地创建大量的模块。
英文:
I have a series of go files that are linked by use but are logically independent. They all use a common set of helper functions defined in a separate file.
My directory structure is shown below.
src/
├── foo1.go
├── foo2.go
├── ...
├── fooN.go
└── helper/
└── helper.go
The foox.go files are all of this form -
package main
import help "./helper"
// functions and structs that use functionality in
// helper but are not related to anything going on
// in other foox.go files
func main() {
// more things that uses functionality in helper
// but are not related to anything going on in
// other foox.go files
return
}
I was running specific files with go run foox.go
, but recently updated my version of Go. This workflow is now broken since relative imports are no longer permitted -
"./helper" is relative, but relative import paths are not supported in module mode
What is the correct the way to structure a collection independent Go files that all depend on the same collection of helper functions?
All the guidance says to use modules, but in this case that will mean having a separate module for each foox.go file where each file contains func
s, struct
s etc. that will never be used in any other module.
All I want to do is be able run a single .go file that includes another single local .go file without going through the hassle of making dozens of modules.
答案1
得分: 0
你可以通过设置环境变量 GO111MODULE=off
来禁用模块模式。
英文:
You can disable module mode by setting the environment variable GO111MODULE=off
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论