英文:
google app engine file conflict golang
问题
所以我正在尝试使用Google的应用引擎运行我的Go应用程序。当我运行goapp server
时,我遇到了这个错误:
go-app-builder: Failed parsing input: app file model.go conflicts with same file imported from GOPATH
这是我的项目布局:
.
├── model
│ └── model.go
├── reqres
│ └── reqres.go
├── app.yaml
├── service.go
├── main.go
└── transport.go
如果我在没有应用引擎的情况下运行它,我不会遇到任何错误,应用程序可以正常运行。
英文:
So I'm trying to run my go app with google's app engine. When I run goapp server
I get this error:
go-app-builder: Failed parsing input: app file model.go conflicts with same file imported from GOPATH
This is my project layout:
.
├── model
│ └── model.go
├── reqres
│ └── reqres.go
├── app.yaml
├── service.go
├── main.go
└── transport.go
If I run it without app engine I don't any get errors and the app runs fine.
答案1
得分: 3
根据我的经验,你之所以会遇到这个错误,是因为你的项目文件夹也在你的GOPATH下面。"goapp"会克隆你的项目文件夹,并根据go环境的GOPATH和GOROOT进行构建。这样做会导致在你的项目中声明的所有包的符号重复。
在go appengine文档中有解释:
如果你将包的源代码包含在GOPATH中,你必须小心,不要将源代码放在包含有app.yaml文件的App Engine项目的任何目录中。如果发生这种情况,一个包可能会被加载两次,一次是相对于模块目录的路径,一次是完全限定的路径。这可能会导致一些微妙的问题,因此Go SDK会扫描你的项目和GOPATH,检测到这种情况,并将其报告为错误。
在同一个链接中,你会找到Google对项目结构的一些建议,其中之一是(你的项目违反了这个指南):
不要在模块目录中包含任何子目录。
如果你想要一个包含应用程序定义和go包的存储库,我建议你采用以下结构:
项目根目录
|- 模块
| |- myModule1
| | |- init.go // 路由模式到处理程序
| | |- myModule1.yaml // 模块的配置
| |- myModule2
| |- init.go // 路由模式到处理程序
| |- myModule2.yaml // 模块的配置
|
|- pkg
| |- myModule1
| | |- *.go // 源代码,子文件夹(包)
| | // 包含处理程序和业务代码
| |- myModule2
| | |- *.go // 源代码,子文件夹(包)
// 包含处理程序和业务代码
这种结构方便且改善了调试体验,如在文章使用Visual Studio Code调试Go appengine模块中所解释的。
英文:
According to my experience you get this error because your project folder is also under your GOPATH. "goapp" kind of clone your project folder and builds it against the go environment GOPATH and GOROOT... Doing so it finds duplicated symbols for all package that you have been declared under your project.
Here is the explanation in go appengine documentation
> If you include your package sources in GOPATH, you must be careful not to place the source code at or below any directories in your App Engine project that contain app.yaml files. If that happens, a package could be loaded twice, once for the path relative to a module's directory, and once for the fully-qualified path. This can cause subtle problems, so the Go SDK scans your project and your GOPATH, detects this case, and reports it as an error.
Under the same link you will find some advises by google for your project structure and one of them is (your project break that guideline):
> Do not include any subdirectories in a module's directory.
If you want a repository with your application definition and go packages I encourage you to adopt the folliwing structure:
projectRoot
|- modules
| |- myModule1
| | |- init.go // router pattern to handler
| | |- myModule1.yaml // configuration for the module
| |- myModule2
| |- init.go // router pattern to handler
| |- myModule2.yaml // configuration for the module
|
|- pkg
| |- myModule1
| | |- *.go // sources, subfolders(packages)
| | // with handlers and business code
| |- myModule2
| | |- *.go // sources, subfolders(packages)
// with handlers and business code
This structure is convinient and improves debugging experience as explained in the article debugging Go appengine module with visual studio code
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论