Run a single Go file with a single local import without using modules

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

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文件都需要一个单独的模块,其中每个文件包含的funcstruct等将永远不会在任何其他模块中使用。

我只想运行一个包含另一个本地.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 funcs, structs 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

huangapple
  • 本文由 发表于 2022年10月30日 04:05:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/74248447.html
匿名

发表评论

匿名网友

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

确定