Vendoring in Go 1.6
Asked Answered
F

1

5

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.

Fuji answered 14/3, 2016 at 22:9 Comment(4)
The whole branch under src should be in a subdirectory, no? Can you try making a directory "foo" and move main.go and vender under it.Sevastopol
Could you please post what exact commands you tried and what error messages you got?Tobitobiah
@Tobitobiah Edited to add build commands.Fuji
@Sevastopol No luck. But is vendoring only for libraries?Fuji
T
9

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 resulting myprogram binary to $GOPATH/bin/myprogram
Tobitobiah answered 16/3, 2016 at 1:15 Comment(2)
Thank you so much @kostya; you solved it for me. There were two things I needed to do: properly set $GOPATH, and move the project to a subdirectory under ./src.Fuji
moving the project to src/ did it for meLeighannleighland

© 2022 - 2024 — McMap. All rights reserved.