使用本地包的部署构建失败,并且“导入路径不是以主机名开头”

使用本地包的部署构建失败,并且“导入路径不是以主机名开头”

问题描述:

I'm new to Go, and I imagine this is an obvious situation for anyone who's worked with Go deployment in the past. In the most TL;DR sense, I built out a demo project from Scotch.IO for a simple todo list using Go (https://scotch.io/tutorials/create-a-single-page-app-with-go-echo-and-vue), and it works flawlessly on my machine, but it breaks in CircleCI when it attempts to import locally defined packages:

  import (
     "database/sql"
     "net/http"
     "strconv"

     "go-todo/models" // <- This

     "github.com/labstack/echo"
  )

I know there's no real reason to deploy an app like this, but I'd like to understand it better before diving into larger projects where deployment is essential. The logs at the breakpoint read:

package go-todo/handlers: unrecognized import path "go-todo/handlers" (import path does not begin with hostname)
package go-todo/models: unrecognized import path "go-todo/models" (import path does not begin with hostname)

And while I grasp that the path is incomplete from a remote environment perspective, I also know that relative paths won't fix it, and I have to assume there's some environmental variable that my ignorance is making me blind to in this context.

It's also worth noting that all of the go get calls for remote packages play nicely as expected.

Any help will be greatly appreciated. Refining my google searches is just bringing back the same stack of seemingly unrelated questions.

You need to have all imported libraries in your $GOPATH (external libs) or $GOROOT (std lib)

The go get command can fetch libraries from remote and put them in your $GOPATH

So if you go get github.com/labstack/echo it fetches the library from the URL and places it in $GOPATH/github.com/labstack/echo locally.

This is not possible for the library go-todo/models to be fetched remotely like that, so you need to make sure in your CI that it is copied and made available locally at $GOPATH/go-todo/models.

Or you can try vendoring.