main.go
LIB/file_1.go
...
package lib
...
type MyStruct struct{
}
....
Run Code Online (Sandbox Code Playgroud)
如何在同一个包/文件夹中的另一个文件中引用"MyStruct"?
我得到了未定义:MyStruct在构建lib/file_2.go时.我可以运行go install而没有错误,我应该忽略构建错误吗?
LIB/file_2.go
...
package lib
...
{
m MyStruct
}
....
Run Code Online (Sandbox Code Playgroud)
谢谢
Emd*_*won 11
这个命令对我有用
go run *.go
Run Code Online (Sandbox Code Playgroud)
实际上这将编译所有go文件并运行你的main函数.所以这很好用
你要求go工具编译lib/file_1.go,你永远不会提到lib/file_2.go它应该怎么知道它应该编译它?
来自go help build:
Build compiles the packages named by the import paths,
along with their dependencies, but it does not install the results.
If the arguments are a list of .go files, build treats them as a list
of source files specifying a single package.
Run Code Online (Sandbox Code Playgroud)
您应该可以MyStruct直接使用它,因为它和它的定义在同一包中。
如果您有任何问题,有时在创建之前可以帮忙(例如,对于SublimeText + GoSublime这样的IDE )。
这样,便会被编译并显示在中,并且在编译期间可以使用定义。go install lib/file_2.golib/file_1.goGOPATH/pkglib/file_1.golib/file_2.go