cannot find package “appengine/cloudsql”

2019-05-31 02:07发布

I develop some GO libraries using Google Cloud SQL and MySQL server. When I imported `appengine/cloudsql, an error below occured.

cloud.go:20:2: cannot find package "appengine/cloudsql" in any of: /usr/local/Cellar/go/1.1.2/src/pkg/appengine/cloudsql (from $GOROOT) /Users/lameduck/myGo/src/appengine/cloudsql (from $GOPATH)

I know this package, appengine/cloudsql, is only for Google App Engine and it doesn't exist on everywhere else.

I'm wondering how can I use it for GAE and standard sql library for other environments in a single library.

PS: I can setup Google App Engine SDK correctly. My question is not relevant to it. I hope my library runs on Google App Engine and standalone environment together. (I already made a code for GAE and a code for other dabatases.) It is Ok that users have to setup some configurations. But I don't want that users have to modify a library source code.

Thanks for any help.

2条回答
走好不送
2楼-- · 2019-05-31 03:10

Package import is done during compile/link time. And Go doesn't support runtime conditional imports in distinction from Python.

Feature you are looking for is dynamic library loading (like in C/C++ you can load .so/.dll in runtime), but Go currently doesn't support it.

查看更多
孤傲高冷的网名
3楼-- · 2019-05-31 03:13

I solved the problem. I used a build constraint to use the proper routine and avoid an error. There is a build constraint for App Engine, appengine.

The App Engine SDK introduces a new build constraint term: "appengine". Files that specify

// +build appengine will be built by the App Engine SDK and ignored by the go tool. Conversely, files that specify

// +build !appengine are ignored by the App Engine SDK, while the go tool will happily build them.

PS: Anway, I upvoted other answers. Thank you.

查看更多
登录 后发表回答