My GOPATH is $HOME/go
, I put my project's source code in $HOME/go/src/myproj
and there are two files:
app.yaml:
application: hello
version: 1
runtime: go
api_version: go1
handlers:
- url: /.*
script: _go_app
and hello.go
package hello
import (
"net/http"
"github.com/gin-gonic/gin"
)
func ping(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
}
func init() {
r := gin.Default()
api := r.Group("/api")
{
api.GET("/ping", ping)
}
http.Handle("/", r)
}
Then I run dev_appserver.py .
It works,
curl http://127.0.0.1:8080/api/ping
{"message":"pong"}
Then I decide to split hello.go this way:
new hello.go
package hello
import (
"net/http"
"github.com/gin-gonic/gin"
"./api"
// "myproj/api" // does not work too
)
func init() {
r := gin.Default()
api.addRoute()
http.Handle("/", r)
}
and a api
folder, and api/api.go
file
package api
import (
"github.com/gin-gonic/gin"
)
func ping(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
}
func addRoute() {
api := r.Group("/api")
{
api.GET("/ping", ping)
}
}
Then I run dev_appserver.py .
again but got this error:
ERROR 2018-09-28 05:17:47,653 instance_factory.py:229] Failed to build Go application: (Executed command: /Users/gaco/.google-cloud-sdk/platform/google_appengine/goroot-1.9/bin/go-app-builder -app_base /Users/gaco/go/src/myproj -api_version go1 -arch 6 -dynamic -goroot /Users/gaco/.google-cloud-sdk/platform/google_appengine/goroot-1.9 -nobuild_files ^^$ -incremental_rebuild -unsafe -print_extras_hash hello.go api/api.go)
2018/09/28 14:17:47 go-app-builder: Failed parsing input: app file api.go conflicts with same file imported from GOPATH
WARNING 2018-09-28 05:17:47,654 instance.py:297] Could not get PID of instance ERROR 2018-09-28 05:17:47,654 instance.py:300] '_GoBuildFailureRuntimeProxy' object has no attribute '_process'
What is the problem? How can I solve it?
I just realized this is a problem I had once in my own App Engine project. The problem is that because your
api
folder is nested beneath yourmyproject
folder. The SDK doesn't handle the imports well and things end up recursively imported, hence the2018/09/28 14:17:47 go-app-builder: Failed parsing input: app file api.go conflicts with same file imported from GOPATH
error.Effectively, you have to make sure you are never importing something down a directory, which is really annoying.
To solve this, given the info here, you would need to break this out into a more flat directory structure. Something like this:
With nothing in the bare
myproject
folder. Then there is no possibility for it to be included recursively.For reference, my own project has a website, api, db, and 3rd party api structs, so my structure is as you'd expect given the solution mentioned. e.g.
Note that both my website and api are separate services, so they both have their own
app.yaml
in their respective directories. You would just have the one. Also in my example, both projects importdb
andexternal
.