gpt4 book ai didi

Golang : Installation directory error?

转载 作者:IT老高 更新时间:2023-10-28 13:02:41 33 4
gpt4 key购买 nike

我有错误:

go install: no install location for directory /Users/xwilly/Dropbox/go/project/src outside GOPATH 

我在 OS X 上使用 go 版本 1.1。

我可以构建和运行,但不能安装包。

我的环境:

GOPATH=/Users/xwilly/Dropbox/go/project
PATH=/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/go/bin:/Users/xwilly/Dropbox/go/project/bin

项目树:

/Users/xwilly/Dropbox/go/project
bin
pkg
src

我可以毫无错误地构建:

..:src xwilly$ go build test.go 
..:src xwilly$ go install test.go
go install: no install location for directory /Users/xwilly/Dropbox/go/project/src outside GOPATH

这是一个简单的例子:

xwilly$ cat test.go 
package main

import (
"fmt"
)

func main() {
fmt.Println("Bonjour")
}
xwilly$ go run test.go
Bonjour
xwilly$ go install test.go
go install: no install location for directory /Users/xwilly/Dropbox/go/project/src/learning outside GOPATH

最佳答案

Command go

GOPATH environment variable

Each directory listed in GOPATH must have a prescribed structure:

The src/ directory holds source code. The path below 'src' determines the import path or executable name.

The pkg/ directory holds installed package objects. As in the Go tree, each target operating system and architecture pair has its own subdirectory of pkg (pkg/GOOS_GOARCH).

If DIR is a directory listed in the GOPATH, a package with source in DIR/src/foo/bar can be imported as "foo/bar" and has its compiled form installed to "DIR/pkg/GOOS_GOARCH/foo/bar.a".

The bin/ directory holds compiled commands. Each command is named for its source directory, but only the final element, not the entire path. That is, the command with source in DIR/src/foo/quux is installed into DIR/bin/quux, not DIR/bin/foo/quux. The foo/ is stripped so that you can add DIR/bin to your PATH to get at the installed commands. If the GOBIN environment variable is set, commands are installed to the directory it names instead of DIR/bin.

Here's an example directory layout:

GOPATH=/home/user/gocode

/home/user/gocode/
src/
foo/
bar/ (go code in package bar)
x.go
quux/ (go code in package main)
y.go
bin/
quux (installed command)
pkg/
linux_amd64/
foo/
bar.a (installed package object)

您的目录结构错误。您正在尝试安装命令(package main)。它应该位于以您的命令命名的源目录中。请参阅上面的 quux 命令。

在您的情况下,假设您的命令将被命名为 billy

$ mkdir -p /Users/xwilly/Dropbox/go/project/src/billy

在你的 GOPATH 中。将您的 test.go 文件移动到此目录。运行

$ go install billy

命令billy应该安装在

中,除非你设置了 GOBIN
/Users/xwilly/Dropbox/go/project/bin

你的 GOPATH 中的目录,它应该在你的 PATH 中。

关于Golang : Installation directory error?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17524392/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com