Home > Backend Development > Golang > Why Can\'t I Import Packages from My Vendor Directory in Go 1.6?

Why Can\'t I Import Packages from My Vendor Directory in Go 1.6?

Barbara Streisand
Release: 2024-10-28 21:55:30
Original
825 people have browsed it

Why Can't I Import Packages from My Vendor Directory in Go 1.6?

Vendoring in Go 1.6: Troubleshooting Import Issues

Understanding the concept of vendoring in Go 1.6 can be challenging. Let's break down a common scenario and provide a solution to assist with importing files using the new vendor feature.

Issue: Despite following documentation and researching online, an individual is unable to import packages using the vendor directory in Go 1.6.

Here's a sample project structure:

.
└── src
    ├── main.go
    └── vendor
        └── github.com
            └── zenazn
                └── goji
                    ├── ...
Copy after login

And the sole file in the project, main.go:

<code class="go">import (
    "github.com/zenazn/goji"
    "github.com/zenazn/goji/web"
)</code>
Copy after login

Environment Variables:

<code class="Bash">export GOPATH=~/.go
export GOBIN=$GOPATH/bin
export PATH=$PATH:/usr/local/opt/go/libexec/bin:$GOBIN</code>
Copy after login

Attempted Build Commands:

  • go run ./src/main.go
  • go build ./src/main.go

Solution:

To resolve this, it's crucial to understand the way Go tools handle source code and GOPATH. To build a Go program:

  1. Create a directory under $GOPATH/src, e.g., mkdir $GOPATH/src/myprogram.
  2. Place all source code (including the vendor directory) there: $GOPATH/src/myprogram/main.go, $GOPATH/src/myprogram/vendor.
  3. Run go install myprogram to compile the application and place the resulting myprogram binary in $GOPATH/bin/myprogram.

Refer to the official documentation at https://golang.org/doc/code.html for a comprehensive understanding of these concepts.

The above is the detailed content of Why Can\'t I Import Packages from My Vendor Directory in Go 1.6?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template