问题描述
在我的GOPATH中,我有这样的东西:
/ bin /
/ pkg /
/ src /
/ src / my_prog /
/src/my_prog/main.go
/src/my_prog/d_interface.go
/src/my_prog/d_struct_that_implements_the_interface.go
在 main.go
我有 package main
,位于 d_interface.go
和 d_struct_that_implements_the_interface.go
我有 package my_prog
。
当我尝试 go build my_prog
I得到以下错误:
无法加载软件包:package my_prog:找到软件包my_prog(d_interface.go)和main(main .go)in C:\dev\Code\Go\src\my_prog
这是否意味着任何属于 package main
的文件都应该放在它自己的文件夹中?如果是这样,这是什么原因?
是的,每个包都必须在自己的目录中定义。
源结构在中定义。
包是一个组件,您可以在多个程序中使用,您可以发布,导入,从URL获取等。因此,它是有意义的拥有自己的目录,就像程序可以拥有一个目录一样。
In my GOPATH I have something like this:
/bin/
/pkg/
/src/
/src/my_prog/
/src/my_prog/main.go
/src/my_prog/d_interface.go
/src/my_prog/d_struct_that_implements_the_interface.go
In main.go
I have package main
, in d_interface.go
and d_struct_that_implements_the_interface.go
I have package my_prog
.
When I try to go build my_prog
I get the following error:
can't load package: package my_prog: found packages my_prog (d_interface.go) and main (main.go) in C:\dev\Code\Go\src\my_prog
Does this mean that any file that belongs to package main
should go in its own folder? If so, what is the reason for this?
Yes, each package must be defined in its own directory.
The source structure is defined in How to Write Go Code.
A package is a component that you can use in more than one program, that you can publish, import, get from an URL, etc. So it makes sense for it to have its own directory as much as a program can have a directory.
这篇关于错误“无法加载软件包:软件包my_prog:找到软件包my_prog和main”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!