douxiuyu2028 2018-03-10 22:15
浏览 35

如何在golang中正确导入本地软件包,并使它仍可与heroku一起使用?

I have an issue with importing a local package in my go project. It seems that while I'm working locally I have to import with the full path: "github.com/username/myproject/mypackage". But when I push to heroku I get an error about not being able to find that package. If I change the import to "myproject/mypackage", it works on heroku, but no longer works locally.

The two solutions I've seen while researching seem like workarounds:

  1. Put my local packages into vendor/. My understanding is that the vendor directory is intended for third party libraries, so I'd rather not misuse that.
  2. Move myproject/ to be directly in src/. All the golang resources seem to stress that the file structure is very important and that I should have a github.com folder. I'm not clear on why the file structure is important, so this is the solution I'm going with right now, but I can't help but think I'm doing something wrong.

Has anyone else encountered this issue? I've included my file structure, .go files, and Godeps.json below for clarity.

My file structure:

$GOPATH
└───bin
└───pkg
└───src
    └───github.com
        └───username
            └───myproject
                │   main.go
                └───Godeps
                │   │   Godeps.json
                └───mypackage
                    │   mypackage.go

mypackage.go

package mypackage

import (
    "fmt"
)

func PrintHelloWorld() {
    fmt.Println("Hello, world!")
}

main.go:

package main

import (
    "fmt"
    "github.com/username/myproject/mypackage" // import causing problems in heroku
    "log"
    "net/http"
    "log"
)

func sayHelloWorld(w http.ResponseWriter, r *http.Request) {
    mypackage.PrintHelloWorld()
    fmt.Fprintf(w, "Hello world!")
}

func main() {
    http.HandleFunc("/", sayHelloWorld)

    err := http.ListenAndServe(":9090", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

Godeps.json:

{
    "ImportPath": "myproject",
    "GoVersion": "go1.10",
    "GodepVersion": "v80",
    "Deps": []
}

Error when pushing to heroku:

-----> Go app detected
-----> Checking Godeps/Godeps.json file.
-----> Using go1.10
 !!    Installing package '.' (default)
 !!
-----> Running: go install -v -tags heroku .
main.go:6:2: cannot find package "github.com/username/myproject/mypackage" in any of:
    /tmp/tmp.xY81RbKUze/.go/src/myproject/vendor/github.com/username/myproject/mypackage (vendor tree)
    /app/tmp/cache/go1.10/go/src/github.com/username/myproject/mypackage (from $GOROOT)
    /tmp/tmp.xY81RbKUze/.go/src/github.com/username/myproject/mypackage (from $GOPATH)
 !     Push rejected, failed to compile Go app.
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 已知平面坐标系(非直角坐标系)内三个点的坐标,反求两坐标轴的夹角
    • ¥15 数据量少可以用MK趋势分析吗
    • ¥15 使用VH6501干扰RTR位,CANoe上显示的错误帧不足32个就进入bus off快慢恢复,为什么?
    • ¥15 大智慧怎么编写一个选股程序
    • ¥100 python 调用 cgps 命令获取 实时位置信息
    • ¥15 两台交换机分别是trunk接口和access接口为何无法通信,通信过程是如何?
    • ¥15 C语言使用vscode编码错误
    • ¥15 用KSV5转成本时,如何不生成那笔中间凭证
    • ¥20 ensp怎么配置让PC1和PC2通讯上
    • ¥50 有没有适合匹配类似图中的运动规律的图像处理算法