go get/build 无法解析本地项目包。

huangapple go评论87阅读模式
英文:

go get/build can't resolve a local-only project package

问题

project.go:6:2: 在任何地方都找不到包"example.com/project/package":
/usr/local/Cellar/go/1.1.2/libexec/src/pkg/example.com/project/package(来自$GOROOT)
/Users/me/go/src/example.com/project/package(来自$GOPATH)

正在获取https://example.com/project/package?go-get=1
忽略状态码为404的https获取
正在获取http://example.com/project/package?go-get=1
从http://example.com/project/package?go-get=1解析元标签(状态码为404)
导入"example.com/project/package":解析http://example.com/project/package?go-get=1时出错:没有go-import元标签
包example.com/project/package:无法识别的导入路径"example.com/project/package"

为什么go get/build找不到本地包。我理解go get会在我的仓库上失败,因为它是裸的,但它似乎完全忽略了本地文件,强制我在编译之前提交和推送我的代码。这是根据代码片段,OSX 10.8和通过brew安装的Go 1.1.2。GOPATH设置为/Users/me/go,GOROOT为空。

我应该注意到,在我的Windows机器上使用gitbash时,我根本没有这个问题。我已经尝试了所有我能想到的谷歌搜索,但每个人都声称你可以使用相对的"project/package"导入,但在这种情况下完全失败。

英文:
project.go:6:2: cannot find package "example.com/project/package" in any of:
	/usr/local/Cellar/go/1.1.2/libexec/src/pkg/example.com/project/package (from $GOROOT)
	/Users/me/go/src/example.com/project/package (from $GOPATH)


Fetching https://example.com/project/package?go-get=1 
ignoring https fetch with status code 404 Fetching http://example.com/project/package?go-get=1 
Parsing meta tags from http://example.com/project/package?go-get=1 (status code 404)     
import "example.com/project/package": parse http://example.com/project/package?go-get=1: no go-import meta tags 
package example.com/project/package: unrecognized import path "example.com/project/package"

Why can't go get/build find the local package. I understand go get will fail on my repo because it's bare, but it seems like go get is completely ignoring the local file, forcing me to commit and push my code before I can compile it. This is, per the snippet, OSX 10.8 and Go 1.1.2 installed via brew. GOPATH is set to /Users/me/go and GOROOT is empty.

I should note that I don't have this problem at all when using go get in gitbash on my Windows machine. I've tried all the google-fu I can think of to search this, but everyone claims you can use relative "project/package" imports, which also completely fail in this case.

答案1

得分: 3

通过brew upgrade go将go版本从go1.1.2升级到go1.2解决了这个问题。

英文:

Upgrading from go1.1.2 to go1.2 through brew upgrade go fixed this problem

答案2

得分: 2

你可以在本地工作时不需要推送代码,只需要确保文件夹结构与导入路径相对应。

将你的库代码放在一个名为src/的目录中,其文件夹结构应与导入路径相对应:

[15:42] foa:home $ tree
.
├── myprogram.go
└── src
    └── example.com
        └── project
            └── mypackage
                └── mypackage.go

然后将GOPATH设置为包含src/目录的路径,这样你就可以构建项目了:

[15:42] foa:home $ export GOPATH=`pwd`
[15:42] foa:home $ go build

这是myprogram.go的内容:

package main

import "example.com/project/mypackage"

func main() {
    mypackage.Run()
}

以及mypackage.go的内容:

package mypackage

import "fmt"

func Run() {
    fmt.Println("It works.")
}

通常情况下,你可以按照这种方式布置你的工作目录,并将实际的git仓库放在最低级别(例如,在src/example.com/project/mypackage中),这样它们就可以与其他用户的go getgo install命令干净地配合使用。

英文:

You can work with code locally without having pushed it - you just need your folder structure to mimic the import path.

Put your library code inside a src/ directory, with a folder structure mimicking the import path:

[15:42] foa:home $ tree
.
├── myprogram.go
└── src
    └── example.com
        └── project
            └── mypackage
                └── mypackage.go

Then set GOPATH to the directory that has src/ in it, and you'll be able to build the project:

[15:42] foa:home $ export GOPATH=`pwd`
[15:42] foa:home $ go build

Here's myprogram.go:

package main

import "example.com/project/mypackage"

func main() {
    mypackage.Run()
}

And mypackage.go:

package mypackage

import "fmt"

func Run() {
    fmt.Println("It works.")
}

It's common to lay out your working directories like this, and then root the actual git repositories at the lowest level (e.g. in src/example.com/project/mypackage) so that they work cleanly with go get and go install for other users.

huangapple
  • 本文由 发表于 2013年12月5日 00:56:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/20381343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定