Google App Engine Golang 导入不起作用

huangapple go评论152阅读模式
英文:

Google App Engine Golang Imports not working

问题

我正在尝试使用Golang的数据存储客户端库,但它无法工作。我通过go -t命令下载了它,也通过下载GitHub文件手动安装了它。下载完成后,我将库文件复制到了我的google-cloud-sdk文件夹中。但是,当我尝试启动dev_appserver.py时,出现了很多缺少的包。我是否需要手动导入它们,还是存在错误?

英文:

I am trying to use the datastore client library for golang, but it won't work. I downloaded it via the go -t command and also manually by downloading the github files. After the download I have copied the libraries into my google-cloud-sdk folder. But now when I try to start the dev_appserver.py there are so many missing packages. Do I have to import them all manually or is there an error?

答案1

得分: 2

你需要设置go环境。看起来你还没有设置go环境。
例如:

11:35 $ go env
...
GOPATH="/Users/.../Goarea"
...
GOROOT="/usr/local/go"

在你的GOPATH中,你会有bin/pkg/src目录。所以当你执行

go get -u cloud.google.com/go/datastore

它会被拉到.../src/cloud.google.com/go/datastore

然后你可以导入pkg并在你的代码中使用它。

package main

import (
    "encoding/json"
    "log"
    "net/http"

    "google.golang.org/api/iterator"
    "google.golang.org/api/option"

    // 导入Google Cloud Datastore客户端包。
    "cloud.google.com/go/datastore"
    "golang.org/x/net/context"
)
...
func main() {
    ctx := context.Background()
    projectID := "your id"
    client, err := datastore.NewClient(ctx,
    projectID, option.WithServiceAccountFile("YOUR CREDENTIAL.json"))
    if err != nil {
        log.Fatalf("Failed to create client: %v", err)
    }
...
...
英文:

You will need to setup go env. It seems you do not have go env setup.
For example:

11:35 $ go env
...
GOPATH="/Users/.../Goarea"
...
GOROOT="/usr/local/go"

Inside you GOPATH you would have bin/pkg/src. So when you do

 go get -u cloud.google.com/go/datastore 

It will be pulled to .../src/cloud.google.com/go/datastore

then you can import pkg and use it in your code.

package main

import (
   "encoding/json"
   "log"
   "net/http"

   "google.golang.org/api/iterator"
   "google.golang.org/api/option"

   // Imports the Google Cloud Datastore client package.
   "cloud.google.com/go/datastore"
   "golang.org/x/net/context"
)
...
func main() {
   ctx := context.Background()
   projectID := "your id"
   client, err := datastore.NewClient(ctx,
   projectID, option.WithServiceAccountFile("YOUR CREDENTIAL.json"))
   if err != nil {
	   log.Fatalf("Failed to create client: %v", err)
   }
...
...

huangapple
  • 本文由 发表于 2017年5月12日 17:02:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/43933605.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定