gpt4 book ai didi

google-app-engine - 在用 golang 编写的 Google App Engine 应用程序中使用和测试本地包的好方法?

转载 作者:IT王子 更新时间:2023-10-29 01:06:37 28 4
gpt4 key购买 nike

我在测试我的 GAE golang 应用程序使用本地包时遇到问题。该项目看起来像这样。

.
├── app.yaml
├── main.go
└── handler
└── handler.go

handler包在main.go中导入。

package main

import "handler"

在我开始编写测试之前,一切(例如,goapp serve)都工作正常。goapp test 提示找不到 handler 包。好像是 GOPATHgoapp servegoapp test 是不同的。我发现的一种解决方案是handler 包在项目路径外并完全导入限定名称(例如,github.com/.../handler),但对我来说没有意义将项目拆分到它们紧密耦合的不同地方。是有什么好的方法可以使用和测试本地包吗?

可找到有关此主题的以下资源。

最佳答案

这来晚了,但如果有人遇到同样的问题,这里是处理方法:

  1. 以这种方式组织代码

    /whereever
    └── /myproject
    └──/src
    ├── app.yaml
    ├── main.go
    └── /handler
    └── handler.go
  2. 为您的项目代码使用不合格的导入,例如

    import handler
  3. 从 src 文件夹中运行 goapp servegoapp deploy。服务/部署与 app.yaml 相关的工作

    cd /whereever/myproject/src
    goapp serve
  4. goapp test 设置 GOPATH,它基于 GOPATH 工作

    cd /whereever/myproject    #GOPATH expects a subfolder called src
    GOPATH=$GOPATH:`pwd` #add the current folder to the GOPATH
    goapp test ./... #run all tests in any subfolders
  5. 必要时重置您的 GOPATH

    source ~/.profile #this assumes ~/.profile is where you have permanently set your GOPATH

总而言之,“按预期工作”,但仍然很痛苦。

关于google-app-engine - 在用 golang 编写的 Google App Engine 应用程序中使用和测试本地包的好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29785752/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com