英文:
Not resolving dependency in vendor directory
问题
我正在运行go1.6,在GOPATH(/Users/bweidlich/Projects/go)中运行"go build"时遇到以下错误:
main.go:6:2: 无法在以下任何位置找到包"github.com/spf13/viper":
/usr/local/go/src/github.com/spf13/viper(来自$GOROOT)
/Users/bweidlich/Projects/go/src/github.com/spf13/viper(来自$GOPATH)
项目结构:
bin/
glide.lock
glide.yaml
go.iml
logs/
main.go
pkg/
src/
vendor/
github.com/
deckarep/gosx-notifier
spf13/viper
gizak/termui
main.go
package main
import (
"fmt"
"github.com/gizak/termui" <--- 无法解析
"github.com/spf13/viper" <--- 无法解析
"log"
"bweidlich/dash"
"net/http"
"os"
"os/exec"
"time"
)
英文:
I'm running go1.6 and am getting the follow error when running "go build" in GOPATH (/Users/bweidlich/Projects/go)
main.go:6:2: cannot find package "github.com/spf13/viper" in any of:
/usr/local/go/src/github.com/spf13/viper (from $GOROOT)
/Users/bweidlich/Projects/go/src/github.com/spf13/viper (from $GOPATH)
Project structure:
bin/
glide.lock
glide.yaml
go.iml
logs/
main.go
pkg/
src/
vendor/
github.com/
deckarep/gosx-notifier
spf13/viper
gizak/termui
main.go
package main
import (
"fmt"
"github.com/gizak/termui" <--- doesn't resolve
"github.com/spf13/viper" <--- doesn't resolve
"log"
"bweidlich/dash"
"net/http"
"os"
"os/exec"
"time"
)
答案1
得分: 5
你的main.go
文件需要放在一个工作空间(即gopath内)中,以便其依赖项可以被供应。作为一个测试,尝试将你的main.go
放在一个虚假的路径中,看看是否可以找到供应的依赖项:
$GOPATH/src/
example.com/
main.go
vendor/
github.com/
spf13/viper/
gizak/termui/
一般来说,你不应该将任何代码存储在gopath的根目录src
之外的位置。也就是说,你应该模仿go get
在创建目录时使用的结构。
英文:
Your main.go
file needs to be inside a workspace (that is, inside gopath) for its dependencies to be vendored. As a test, try putting your main.go
inside a fake path and see if the vendored deps are found:
$GOPATH/src/
example.com/
main.go
vendor/
github.com/
spf13/viper/
gizak/termui/
In general, you don't want to store any code in your gopath outside its root src
directory. That is, you should echo the structure that go get
would use when creating the directories.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论