登录
首页 >  Golang >  Go问答

无法在 App Engine 标准环境中部署 go1.11:应用程序不在 GOPATH 中

来源:stackoverflow

时间:2024-03-09 21:45:25 445浏览 收藏

积累知识,胜过积蓄金银!毕竟在Golang开发的过程中,会遇到各种各样的问题,往往都是一些细节知识点还没有掌握好而导致的,因此基础知识点的积累是很重要的。下面本文《无法在 App Engine 标准环境中部署 go1.11:应用程序不在 GOPATH 中》,就带大家讲解一下知识点,若是你对本文感兴趣,或者是想搞懂其中某个知识点,就请你继续往下看吧~

问题内容

我尝试将我的 go 应用部署到 app engine。我有以下构建错误:

starting step #1 - "builder"
step #1 - "builder": pulling image: gcr.io/gae-runtimes/go111_app_builder:go111_1_11_2_20181111_rc00
step #1 - "builder": go111_1_11_2_20181111_rc00: pulling from gae-runtimes/go111_app_builder
step #1 - "builder": digest: sha256:51fb36bfa16e7013356867c3a3972986084df93e56258fc258579a5799f0436e
step #1 - "builder": status: downloaded newer image for gcr.io/gae-runtimes/go111_app_builder:go111_1_11_2_20181111_rc00
step #1 - "builder": 2018/11/24 18:13:29 your app is not on your gopath, this build may fail.
step #1 - "builder": 2018/11/24 18:13:29 building from go source in /tmp/staging477638319/srv, with main package at ./...
step #1 - "builder": 2018/11/24 18:13:29 building /tmp/staging477638319/srv, saving to /tmp/staging477638319/usr/local/bin/start
step #1 - "builder": 2018/11/24 18:13:30 wrote build output to /builder/outputs/output
step #1 - "builder": 2018/11/24 18:13:30 failed to build app: your app is not on your gopath, please move it there and try again.
step #1 - "builder": building app with command '[go build -o /tmp/staging477638319/usr/local/bin/start ./...]', env '[path=/go/bin:/usr/local/go/bin:/builder/google-cloud-sdk/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin hostname=d253e517b16c home=/builder/home builder_output=/builder/outputs debian_frontend=noninteractive goroot=/usr/local/go/ gopath=/go gopath=/tmp/staging477638319/srv/gopath]': err=exit status 1, out=srv/main.go:7:2: cannot find package "cloud.google.com/go/firestore" in any of:
step #1 - "builder": /usr/local/go/src/cloud.google.com/go/firestore (from $goroot)
step #1 - "builder": /tmp/staging477638319/srv/gopath/src/cloud.google.com/go/firestore (from $gopath)
step #1 - "builder": srv/main.go:8:2: cannot find package "github.com/gin-gonic/gin" in any of:
step #1 - "builder": /usr/local/go/src/github.com/gin-gonic/gin (from $goroot)
step #1 - "builder": /tmp/staging477638319/srv/gopath/src/github.com/gin-gonic/gin (from $gopath)
step #1 - "builder": srv/main.go:9:2: cannot find package "google.golang.org/api/option" in any of:
step #1 - "builder": /usr/local/go/src/google.golang.org/api/option (from $goroot)
step #1 - "builder": /tmp/staging477638319/srv/gopath/src/google.golang.org/api/option (from $gopath)
finished step #1 - "builder"
error
error: build step 1 "gcr.io/gae-runtimes/go111_app_builder:go111_1_11_2_20181111_rc00" failed: exit status 1

我的 app.yaml 文件如下所示:

runtime: go111
handlers:
- url: /api/user
  script: auto

- url: /favicon.ico
  static_files: build/favicon.ico
  upload: build/favicon.ico

- url: /
  static_files: build/index.html
  upload: build/index.html

- url: /
  static_dir: build

根目录下的 main.go 文件如下所示:

package main

import (
    "context"
    "net/http"

    "cloud.google.com/go/firestore"
    "github.com/gin-gonic/gin"
    "google.golang.org/api/option"
)

const firestoreAccountFile = "firebase.json"
const firestoreProjectID = "golang-gae-firestore-template"

type formData struct {
    Name  string `json:"name" binding:"required"`
    Email string `json:"email" binding:"required"`
}

func main() {
    // Gin init
    r := gin.Default()
    // Serve from static build directory
    r.StaticFS("/", http.Dir("./build"))
    // routes
    r.POST("/api/user", userHandler)
    // run application on port 8080
    r.Run(":8080")
}

func writeLogIfError(c *gin.Context, err error) {
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    }
}

func getNewFirestoreClient(ctx context.Context) (*firestore.Client, error) {
    return firestore.NewClient(ctx, firestoreProjectID, option.WithServiceAccountFile(firestoreAccountFile))
}

func userHandler(c *gin.Context) {
    ctx := context.Background()

    client, err := getNewFirestoreClient(ctx)
    writeLogIfError(c, err)
    defer client.Close()
    // Get form data
    var form formData
    c.BindJSON(&form)

    // [START add user to firestore]
    _, _, err = client.Collection("users").Add(ctx, map[string]interface{}{
        "name":  form.Name,
        "email": form.Email,
    })
    if err != nil {
        c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
        return
    }
    // [END add user to firestore]
    c.JSON(http.StatusOK, gin.H{"status": "user added to db"})
}

如果没有出现这些构建错误,我就无法成功构建。我尝试完全遵循 app engine 上的 go 文档,但如果我要构建的应用程序与建议的不同,我会感到困惑。关于如何解决此 gopath 错误有什么想法吗?


解决方案


更新:我无法使用 GOPATH 成功部署,而是在包含环境变量后能够使用 go.mod 成功部署:export GO111MODULE=on 以使模块正常工作。此处的文档:https://github.com/golang/go/wiki/Modules

到这里,我们也就讲完了《无法在 App Engine 标准环境中部署 go1.11:应用程序不在 GOPATH 中》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>