Home  >  Article  >  Backend Development  >  Why is Importing with Vendoring in Go 1.6 Difficult for Some?

Why is Importing with Vendoring in Go 1.6 Difficult for Some?

DDD
DDDOriginal
2024-10-28 22:38:30945browse

Why is Importing with Vendoring in Go 1.6 Difficult for Some?

Importing with Vendoring in Go 1.6

Despite extensive documentation and community assistance, importing using the vendor feature in Go 1.6 has proven elusive for some.

Question:

A developer struggled to import using the vendor feature with a sample project structured as follows:

Directory Structure:

.
└── 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()
}

Environment Variables:

export GOPATH=~/.go
export GOBIN=$GOPATH/bin
export PATH=$PATH:/usr/local/opt/go/libexec/bin:$GOBIN

Answer:

A fundamental understanding of how Go tools handle source code and GOPATH is crucial. Here's how to import with vendoring:

  • Create a directory under $GOPATH/src, e.g.: mkdir $GOPATH/src/myprogram
  • Place the source code and vendor directory within the created directory: $GOPATH/src/myprogram/main.go and $GOPATH/src/myprogram/vendor
  • Execute go install myprogram to build the application and place the binary in $GOPATH/bin/myprogram

The above is the detailed content of Why is Importing with Vendoring in Go 1.6 Difficult for Some?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn