英文:
Vendoring in Go 1.6
问题
我已经阅读了尽可能多的文档和StackOverflow文章,但是在Go 1.6中使用新的供应商功能导入时没有成功。
这是一个我用Goji创建的示例项目,目录结构如下:
.
└── src
├── main.go
└── vendor
└── github.com
└── zenazn
└── goji
├── LICENSE
├── README.md
├── bind
├── default.go
├── example
├── goji.go
├── graceful
├── serve.go
├── serve_appengine.go
└── web
而main.go
,项目中唯一的文件,如下所示:
package main
import (
"fmt"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
)
func hello(c web.C, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"])
}
func main() {
goji.Get("/hello/:name", hello)
goji.Serve()
}
我的环境变量如下:
export GOPATH=~/.go
export GOBIN=$GOPATH/bin
export PATH=$PATH:/usr/local/opt/go/libexec/bin:$GOBIN
我尝试了最简单的构建命令,但没有成功:
go run ./src/main.go
go build ./src/main.go
我还尝试了以下构建命令:
$GOPATH=`pwd`
...但没有成功。我是不是完全忽略了什么?任何建议都将不胜感激。
英文:
I’ve read as many docs and StackOverflow articles as I can find, yet I am having no luck importing using the new vendor feature in Go 1.6.
Here's a sample project I put together with Goji to test. The directory structure is as such:
.
└── src
├── main.go
└── vendor
└── github.com
└── zenazn
└── goji
├── LICENSE
├── README.md
├── bind
├── default.go
├── example
├── goji.go
├── graceful
├── serve.go
├── serve_appengine.go
└── web
And main.go
, the sole file in the project, is as such:
package main
import (
"fmt"
"net/http"
"github.com/zenazn/goji"
"github.com/zenazn/goji/web"
)
func hello(c web.C, w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, %s!", c.URLParams["name"])
}
func main() {
goji.Get("/hello/:name", hello)
goji.Serve()
}
My environment variables are as such:
export GOPATH=~/.go
export GOBIN=$GOPATH/bin
export PATH=$PATH:/usr/local/opt/go/libexec/bin:$GOBIN
I’ve tried the most simple build commands, with no luck:
go run ./src/main.go
go build ./src/main.go
I’ve also attempted to build with:
$GOPATH=`pwd`
...to no avail. Am I totally missing something? Any advice is appreciated.
答案1
得分: 9
我建议你阅读https://golang.org/doc/code.html。需要一两天的时间来理解,但是一旦你理解了go工具如何与源代码和GOPATH一起工作,使用它们就非常容易。
回到你的问题。要构建一个简单的go程序,你需要:
- 在$GOPATH/src下创建一个目录,例如
mkdir $GOPATH/src/myprogram
- 将所有的源代码(包括vendor目录)放在那里:
$GOPATH/src/myprogram/main.go
,$GOPATH/src/myprogram/vendor
。 - 运行
go install myprogram
来构建你的应用程序,并将生成的myprogram
二进制文件放在$GOPATH/bin/myprogram
中。
英文:
I suggest you to read https://golang.org/doc/code.html. It requires a day or two to digest but after you understand how go tools work with the source code and GOPATH it is really easy to use them.
Back to your question. To build a simple go program you need to:
- create directory under $GOPATH/src, e.g.
mkdir $GOPATH/src/myprogram
- put all the source code (including vendor directory) there:
$GOPATH/src/myprogram/main.go
,$GOPATH/src/myprogram/vendor
. - run
go install myprogram
to build your application and put the resultingmyprogram
binary to$GOPATH/bin/myprogram
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论