为了账号安全,请及时绑定邮箱和手机立即绑定

为什么“go run”命令在主包中找不到第二个文件?

为什么“go run”命令在主包中找不到第二个文件?

Go
当年话下 2021-08-30 15:09:06
我遇到了go run main.go产生错误的问题:# command-line-arguments./main.go:9: undefined: test但是,这些命令go build && ./goruntest可以很好地编译和运行程序。输出是:你好来自测试()您好来自 sameFileTest()你好来自 pkgtest.Test()你好来自 pkgtest.Test1()我的目录设置如下:go/src/github.com/username/goruntest/    pkgtest/        pkgtest.go        pkgtest1.go    main.go    test2.go这是代码。main.gopackage mainimport (    "fmt"    "github.com/username/goruntest/pkgtest")func main() {    fmt.Println(test())           // main.go:9    fmt.Println(sameFileTest())    fmt.Println(pkgtest.Test())    fmt.Println(pkgtest.Test1())}func sameFileTest() string {    return "Hi from sameFileTest()"}gotest1.gopackage mainfunc test() string {    return "Hi from test()"}pkgtest/pkgtest.gopackage pkgtestfunc Test() string {    return "Hi from pkgtest.Test()"}pkgtest/pkgtest1.gopackage pkgtestfunc Test1() string {    return "Hi from pkgtest.Test1()"}我知道问题出在第二个文件中package main,我也明白没有真正的理由在main.我的问题是:为什么go run无法处理此设置,但构建和运行可执行文件却能正常工作?编辑包含第二个文件 pkgtest我也明白该命令go run main.go gotest1.go有效,但为什么我需要指定gotest1.go?为了简洁起见,我最初省略了这些细节。但现在我看到它们对这个问题很重要。
查看完整描述

2 回答

?
偶然的你

TA贡献1841条经验 获得超3个赞

尝试将所有相关文件提供给 go run


$ go help run

usage: go run [build flags] [-exec xprog] gofiles... [arguments...]


Run compiles and runs the main package comprising the named Go source files.

A Go source file is defined to be a file ending in a literal ".go" suffix.


查看完整回答
反对 回复 2021-08-30
?
大话西游666

TA贡献1817条经验 获得超14个赞

我想在这里分享一些有用的资源以供参考,这是我从另一个类似的已删除帖子中学到的。它看起来像一个简单的命令,但您可能并不完全了解它。


如果您向 提供源文件列表go run,则您正在创建单个综合包,并且会忽略构建约束。因此,与其列出 go 文件,不如使用导入路径或文件系统路径,例如go run .. 检查它go help packages:

As a special case, if the package list is a list of .go files from a

single directory, the command is applied to a single synthesized

package made up of exactly those files, ignoring any build constraints

in those files and ignoring any other files in the directory.

可执行目标以第一个源文件命名。在此处(第 80 行和第 125 行)和此处(第 2506 行)的go run命令实现中检查它:

// GoFilesPackage creates a package for building a collection of Go files

// (typically named on the command line). The target is named p.a for

// package p or named after the first Go file for package main.

func GoFilesPackage(ctx context.Context, gofiles []string) *Package {

...

为避免包初始化期间的潜在问题,建议您按词法文件名顺序提供 go 文件列表。在规格中检查它:

The declaration order of variables declared in multiple files is determined by the order in which the files are presented to the compiler: Variables declared in the first file are declared before any of the variables declared in the second file, and so on.

...

A package with no imports is initialized by assigning initial values to all its package-level variables followed by calling all init functions in the order they appear in the source, possibly in multiple files, as presented to the compiler.

...

To ensure reproducible initialization behavior, build systems are encouraged to present multiple files belonging to the same package in lexical file name order to a compiler. 

有关于历史的讨论go run,请在此处和此处查看。


查看完整回答
反对 回复 2021-08-30
  • 2 回答
  • 0 关注
  • 341 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信